0

There are no errors but the form doesn't save to database. Otherwise, its possible to add data from django admin.

The urls and redirects also work well, i can be able to open the form, edit/put data and hit submit, but the database does not reflect.

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class RPAS(models.Model):
    user = models.OneToOneField(User)
    manufacturer = models.CharField(max_length = 100, default = '')
    model_name = models.CharField(max_length = 100, default = '')
    serial_no = models.CharField(max_length = 50, default='')
    reg_mark = models.CharField(max_length = 50, default='')

    def __str__(self):
        return self.user.rpas.manufacturer

# RPAS.profile = property(lambda u: RPAS.objects.get_or_create(user=u)[0])

def edit_rpas_profile(sender, **kwargs):
    if kwargs['created']:
        user_profile = RPAS(user=kwargs['instance'])

post_save.connect(edit_rpas_profile, sender=User)

views.py

from .forms import RPASForm
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied

def edit_rpas(request):
        if request.user.is_authenticated():
            if request.method == "POST":
                form = RPASForm(request.POST,instance=request.user)
                if form.is_valid():
                    form.save(commit=False)
                    return redirect ('/account/rpas/')

            else:
                form = RPASForm(instance=request.user)
                args = {'form': form}
                return render(request, "accounts/edit_rpas.html", args)
        else:
            raise PermissionDenied

forms.py

from django import forms
from django.contrib.auth.models import User

from django.forms import ModelForm
from accounts.models import RPAS

class RPASForm(ModelForm):
    class Meta:
        model = RPAS
        fields = (
        'manufacturer',
        'model_name',
        'serial_no'
        )

template

<form action="." method="POST" class="padding">
            {% csrf_token %}

            {{ form.as_p }}

            <input type="submit" value="Submit">

</form>

and finally this is my admin.py

from .models import RPAS
class RpasAdmin(admin.ModelAdmin):
    list_display = ('manufacturer','model_name','reg_mark','serial_no')

    def user_info(self, obj):
        return obj.description

admin.site.register(RPAS, RpasAdmin)
1
  • 1
    commit=False means won't save. Commented Oct 18, 2017 at 12:02

1 Answer 1

2

You are saving with commit=False, so the instance is never saved to the database. Remove it. You should also pass in the RPAS instance, not the User, since the form is for the RPAS model.

if request.method == "POST":
    form = RPASForm(request.POST,instance=request.user.rpas)
    if form.is_valid():
        form.save()
        return redirect ('/account/rpas/')
else:
    form = RPASForm(instance=request.user.rpas)

You only need commit=False if you want to edit the object before saving, for example to set the user field. You don't need to do that in this case, because the user field is already set.

if form.is_valid():
    instance = form.save(commit=False)
    instance.user = request.user
    instance.save()
    return redirect ('/account/rpas/')
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you!! it works. But 'form = RPASForm(instance=request.user.rpas)' only works because the userprofile and rpas models are in the same app. I decided to move the RPAS model to another app. now i get the error that user has no attribiute rpas when using 'form = RPASForm(instance=request.user.rpas)'
request.user.rpas works because you have a one to one field from RPAS to User, so you can also follow the relationship backwards. The two models don't have to be in the same app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.