How to save many to many relationship?

eneepo

I'm almost using following code(I striped down a little bit), I use auto complete light to load users and users can insert different people name(users) seperated by comma. the problem is when I try to save I get following error

ValueError at /write/
"<Article: test1>" needs to have a value for field "article" before this many-to-many relationship can be used.

models.py

class Article(models.Model):

    author = models.ForeignKey(User,)
    people = models.ManyToManyField(User, related_name="with", null=True,)
    content = models.TextField()

forms.py

class ArticleForm(forms.ModelForm):
    people =  forms.CharField(widget=autocomplete_light.TextWidget('UserAutocomplete'))
    class Meta:
        model = Article

views.py

def write(request):
    if request.POST:
        form = ArticleForm(request.POST)
        if form.is_valid():

            content = form.cleaned_data['content']
            user = User.objects.get(username=request.user.username)

            people_str = form.cleaned_data['accompanied']
            people = [x.strip() for x in accompanied_str.split(',')]

            article = Article(
                content = content,
                author = user,
                )
            for username in accompanied:
                user = User.objects.get(username=username)
                article.people.add(user)

            article.save()

            return HttpResponseRedirect('/success/')
kanu

In order to create a relation you need the ids of both side. The newly created article has no id yet. If you save the article first and then add people to it it should work fine.

article = Article(
    content = content,
    author = user,
    )

article.save()

article.add(*User.objects.filter(username__in=accompanied))

The process of adding people can be cheaper by getting all users that have a username from the list of accompanied in one fetch.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to save tag with many to many relationship in ActiveRecord

From Dev

how to save Many to Many relationship in django

From Dev

how to save Many to Many relationship in django

From Dev

Relationship Many-To-One how to save

From Dev

JPA : How to save list with order in a many-to-many relationship

From Dev

strongloop loopback: how to save many-to-many relationship

From Dev

How to save entries in many to many polymorphic relationship in Laravel?

From Dev

How to save models with many-to-many relationship in Ember.js?

From Dev

Bookshelf.js - how to save many-to-many relationship?

From Dev

how to delete or save a many to many relationship in hibernate & spring

From Dev

How to save models with many-to-many relationship in Ember.js?

From Dev

how to flatten many to many relationship

From Dev

how to query a many to many relationship?

From Dev

how to flatten many to many relationship

From Java

Laravel save / update many to many relationship

From Dev

Laravel save one to many relationship

From Dev

How to save a detached entity and replace the content of a one to many relationship?

From Dev

How to save multiple entity in one action related in Many to Many Relationship [Spring Boot 2, JPA, Hibernate, PostgreSQL]

From Dev

How to set strong parameter for many to many relationship

From Dev

How to get many to many relationship items in laravel

From Dev

How many to many relationship in Entity Framework is working

From Dev

How to write activemodel serializer for many to many relationship?

From Dev

SQLAlchemy. How to order on many to many relationship?

From Dev

How to use checkboxes in a many-to-many relationship?

From Dev

jpa - How to create a many to many relationship with an IdClass?

From Dev

how to define many to many relationship in laravel models?

From Dev

How to insert data in many to many relationship

From Dev

how to update foreign key in many to many relationship

From Dev

How to build a Create View for a Many to Many relationship

Related Related

  1. 1

    How to save tag with many to many relationship in ActiveRecord

  2. 2

    how to save Many to Many relationship in django

  3. 3

    how to save Many to Many relationship in django

  4. 4

    Relationship Many-To-One how to save

  5. 5

    JPA : How to save list with order in a many-to-many relationship

  6. 6

    strongloop loopback: how to save many-to-many relationship

  7. 7

    How to save entries in many to many polymorphic relationship in Laravel?

  8. 8

    How to save models with many-to-many relationship in Ember.js?

  9. 9

    Bookshelf.js - how to save many-to-many relationship?

  10. 10

    how to delete or save a many to many relationship in hibernate & spring

  11. 11

    How to save models with many-to-many relationship in Ember.js?

  12. 12

    how to flatten many to many relationship

  13. 13

    how to query a many to many relationship?

  14. 14

    how to flatten many to many relationship

  15. 15

    Laravel save / update many to many relationship

  16. 16

    Laravel save one to many relationship

  17. 17

    How to save a detached entity and replace the content of a one to many relationship?

  18. 18

    How to save multiple entity in one action related in Many to Many Relationship [Spring Boot 2, JPA, Hibernate, PostgreSQL]

  19. 19

    How to set strong parameter for many to many relationship

  20. 20

    How to get many to many relationship items in laravel

  21. 21

    How many to many relationship in Entity Framework is working

  22. 22

    How to write activemodel serializer for many to many relationship?

  23. 23

    SQLAlchemy. How to order on many to many relationship?

  24. 24

    How to use checkboxes in a many-to-many relationship?

  25. 25

    jpa - How to create a many to many relationship with an IdClass?

  26. 26

    how to define many to many relationship in laravel models?

  27. 27

    How to insert data in many to many relationship

  28. 28

    how to update foreign key in many to many relationship

  29. 29

    How to build a Create View for a Many to Many relationship

HotTag

Archive