Django ModelForm not saving data to database

前端 未结 6 2075
温柔的废话
温柔的废话 2021-02-10 05:53

A Django beginner here having a lot of trouble getting forms working. Yes I\'ve worked through the tutorial and browsed the web a lot - what I have is mix of what I\'m finding h

相关标签:
6条回答
  • 2021-02-10 06:18

    This is kind of old thread, but I hope this will help someone. I've been struggling with similar issue, I initiated object from db, updated field, called save() function and nothing happened.

    user = User.objects.get(username = example_string)
    user.userprofile.accepted_terms = True
    user.userprofile.save()
    

    The field accepted_terms have been added lately and that was the issue. First try python manage.py makemigrations and python manage.py migrate, if this does not help you need to flush the database.

    Solution in my case was flushing the database.

    0 讨论(0)
  • 2021-02-10 06:20

    I think you need to 1st create an object for ModelForm and then assign the object on if the request method is post. In your code you are only doing this for only one attribute and before checking the method. -Also in your forms.py ,in fields parameter of Meta() class, you used [] brackets which indicates what attributes must be excluded, but as your including all those attributes in form you must use () brackets. I think your code should be edited as follows:

    see here

    0 讨论(0)
  • 2021-02-10 06:21
    UserDetailsForm(request.POST, instance = db_obj_form)
    

    db_obj should be an object of table UserProfile that you want to save.

    0 讨论(0)
  • 2021-02-10 06:22

    I'm not sure exactly what the problem is here, but one issue certainly is that you are passing instance=request.user when instantiating the form. That's definitely wrong: request.user is an instance of User, whereas the form is based on UserProfile.

    Instead, you want to do something like this:

    f = UserDetailsForm(request.POST)
    if f.is_valid():
        profile = f.save(commit=False)
        profile.user = request.user
        profile.save()
    

    As regards ForeignKey vs OneToOne, you should absolutely be using OneToOne. The advice on using ForeignKeys was given for a while in the run-up to the release of Django 1 - almost five years ago now - because the original implementation of OneToOne was poor and it was due to be rewritten. You certainly won't find any up-to-date documentation advising the use of FK here.

    Edit after comments The problem now is that you're instantiating the form with the first parameter, data, even when the request is not a POST and therefore request.POST is an empty dictionary. But the data parameter, even if it's empty, takes precedence over whatever is passed as initial data.

    You should go back to the original pattern of instantiating the form within the if statement - but be sure not to pass request.POST when doing it in the else clause.

    0 讨论(0)
  • 2021-02-10 06:23

    Wherever you try to submit a form data to your Django code, then the Django will createan object to entire form data. The object is look like this form_obj = request.method_name. Here method_name is the form method that is (POST or GET) ex:- form_obj = request.POST

    If you want to get the form data then use a function request.method_name.get(name) here name is form attribute assign value

    ex:-

    <form method="post">
    {% csrf_token %}
    {form}
    </form>
    

    in view :-

    import statements as you required
    def your_function(request):
      # you can creat a object to the form by
      form_obj = myform()
      if request.method =="POST":
        form_obj = myform(request.POST)
        if form_obj.is_valid():
    
          # you can directly save by use commit()
          form_obj.save(commit=True)
           #   or
          form_obj1 = form_obj.save(commit=False)
          #which  do you want to save in database ,those fields you need to specify like below 
          form_obj1.attribute1 = form_obj.cleaned_data["name of form field"]
            '
            '
          form_obj1.save()
    

    and in Myform.py file

    import statments    
    class myform(forms.ModelForm):
        class meta():
            model = your_models_name
            fields =("attribute1","attribute2".....) # included attribute of your_model
    
    0 讨论(0)
  • 2021-02-10 06:33

    Remember to register your model form to admin.py file. This is a common mistake

    Register your models here.

    from . models import advancedUserForm
    admin.site.register(advancedUserForm)
    
    0 讨论(0)
提交回复
热议问题