Why Form is not saving the many to many field value

Sunder R

The problem with the code is that the form is sucessfully getting passed but the model is not saving the many to many field in the Profile models. All the values are getting saved except the food and type. Please help me with the code. Model

class Relationship(models.Model):
    status = models.CharField(max_length =30)

    def __str__(self):
        return self.status

class Food(models.Model):
    food_name = models.CharField(max_length = 80)

    def __str__(self):
        return self.food_name

class City(models.Model):
    city = models.CharField(max_length = 80)

    def __str__(self):
        return self.city

class Type(models.Model):
    types = models.CharField(max_length =80)

    def __str__(self):
        return self.types

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    avatar = models.ImageField(upload_to = image_upload_to)
    city = models.OneToOneField(City)
    fav_food = models.ManyToManyField(Food)
    relationship = models.OneToOneField(Relationship)
    preferrs = models.ManyToManyField(Type)

    def get_absolute_url(self):
         return reverse('chatbot')

    def __str__(self):
        return self.user.get_username()
`

And my Model form is as follow

class ProfileForm(ModelForm):
    class Meta:
        model=Profile
        fields = ['avatar','city','fav_food','relationship','preferrs']

and my view is as follow

def ProfileCreate(request):
    template="profile.html"
    context={}
    if request.user.get_username()=='':
        return redirect('account_login')
    if Profile.objects.all().filter(user__username=request.user.get_username()).count()>0:
        return redirect('chatbot')
    else:

        form = ProfileForm(request.POST ,request.FILES or None)

        if form.is_valid():

            new_profile = form.save(commit=False)

            user = User.objects.all().filter(username=request.user.get_username())[0]
            new_profile.user=user
            new_profile.save()
            return redirect('chatbot')

        context['form']=form


    return render(request,template,context)
Alasdair

Since you are saving the form with commit=False, you must call the form's save_m2m method after saving the instance.

new_profile = form.save(commit=False)
# no need to fetch the user here, just use request.user
new_profile.user = request.user
new_profile.save()
form.save_m2m()

See the docs on the model form save method for more info.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Many Many Extra Field not saving

From Dev

Many Many Extra Field not saving

From Dev

Saving Django formset with Many to Many Field

From Dev

Symfony Form not saving data in many to many Association

From Dev

Symfony Form not saving data in many to many Association

From Dev

Django Save Many to Many Field - no default value

From Dev

Sequelize saving many to many

From Dev

Sequelize saving many to many

From Dev

Symfony2 entity form type not saving many to many

From Dev

Why is my Entity Framework Many to Many Table not saving automatically?

From Dev

Symfony2 form entity field with many-to-many relationship

From Dev

Hibernate Many-To-Many. Not saving

From Dev

I can't add a field inside a one2many tree (in a form view). Why?

From Dev

get extra field value in Django's many-to-many relationship

From Dev

How to join many to many relationship on max value for relationship field

From Dev

Check if user has chosen a value from many to many field

From Dev

Default Value Many To Many Field doesn't work, Django 1.8

From Dev

Has_many through checkboxes (simple_form) not saving

From Dev

Sort by a value in a many to one field with a django queryset?

From Dev

Update automatically a specific field in Django model when saving other many-to-many related model

From Dev

Spring and/or Hibernate: Saving many-to-many relations from one side after form submission

From Dev

Many to many Field - Accessing attributes

From Dev

Sql to object, many to many with field

From Dev

Sql to object, many to many with field

From Dev

auto update many to many field

From Dev

Why is setting a field many times slower than getting a field?

From Java

Many to many fields in widget form

From Dev

breeze: many-to-many issues when saving

From Dev

Saving data many-to-many relationship

Related Related

  1. 1

    Many Many Extra Field not saving

  2. 2

    Many Many Extra Field not saving

  3. 3

    Saving Django formset with Many to Many Field

  4. 4

    Symfony Form not saving data in many to many Association

  5. 5

    Symfony Form not saving data in many to many Association

  6. 6

    Django Save Many to Many Field - no default value

  7. 7

    Sequelize saving many to many

  8. 8

    Sequelize saving many to many

  9. 9

    Symfony2 entity form type not saving many to many

  10. 10

    Why is my Entity Framework Many to Many Table not saving automatically?

  11. 11

    Symfony2 form entity field with many-to-many relationship

  12. 12

    Hibernate Many-To-Many. Not saving

  13. 13

    I can't add a field inside a one2many tree (in a form view). Why?

  14. 14

    get extra field value in Django's many-to-many relationship

  15. 15

    How to join many to many relationship on max value for relationship field

  16. 16

    Check if user has chosen a value from many to many field

  17. 17

    Default Value Many To Many Field doesn't work, Django 1.8

  18. 18

    Has_many through checkboxes (simple_form) not saving

  19. 19

    Sort by a value in a many to one field with a django queryset?

  20. 20

    Update automatically a specific field in Django model when saving other many-to-many related model

  21. 21

    Spring and/or Hibernate: Saving many-to-many relations from one side after form submission

  22. 22

    Many to many Field - Accessing attributes

  23. 23

    Sql to object, many to many with field

  24. 24

    Sql to object, many to many with field

  25. 25

    auto update many to many field

  26. 26

    Why is setting a field many times slower than getting a field?

  27. 27

    Many to many fields in widget form

  28. 28

    breeze: many-to-many issues when saving

  29. 29

    Saving data many-to-many relationship

HotTag

Archive