0

I want to create the relationship between a person and the news article it got mentioned in.

person/models.py

class Person(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=40)
    news_mentioned = models.ManyToManyField(News, blank=True)

news/models.py

class News(models.Model):
    title = models.CharField(max_length=250)
    abstract = models.TextField(max_length=1000, null=True, blank=True)
    description = models.TextField(max_length=5000, null=True, blank=True)
    url = models.URLField(unique=True)
    ne_person = models.CharField(max_length=250, blank=True, null=True)

Extract from the celery task:

article = News.objects.get(pk=pk)
person = Person.objects.update_or_create(first_name=fname, last_name=lname)
person.save()
mentioned = Person.objects.get(person.id).news_mentioned(article.id)

What am I making wrong here

Sayse
  • 42,633
  • 14
  • 77
  • 146
PSc
  • 46
  • 5
  • Can you provide sample data and input or output so anyone that wants to help can see where the data is wrong? – akousmata Jan 13 '17 at 17:35
  • [link](https://d1ro8r1rbfn3jf.cloudfront.net/ms_102663/D7Xg7IBhd2pztziZpbaoLI1BIEJeoM/pgAdmin%2B4%2B2017-01-13%2B18.53.30.jpg?Expires=1484416462&Signature=eRif7RT0aXCgE4tjbiGjtpy-PY9wCJ2rgoc7Lwla3ZWpEQ7TSzoVsRWdR4~GEU8M10vhOYbWUjrP8Pe9S27IXkbtbwFLdSc9NodKbR8OAsGFcrzsFaI9CNzBIZ7uTtqzJMTAWWBMuiw~ntQLI25EyVPsYSzPBGau38Rl7uxs64tKaXgA6iPAl3aBwkGnFIdYAbHKWUk8Nt9fzS~OEK2LAHpIahq9n8A4fdeE6XQgd7pDS~yF1AvMcNUiDmWxWAmYPqKmIIZhMKFMK2-xXPUt9DLyF5grdaMz~d8Ja0p-wLZdW6WDRW07k-aas5WjTaj~j~c9yJun5vSZE~5~RjCYrg__&Key-Pair-Id=APKAJHEJJBIZWFB73RSA) It creates the person, but not the one to the article. – PSc Jan 13 '17 at 17:55

2 Answers2

1

The models are correct. To add an Article to the Person you need to use .add()

s. https://docs.djangoproject.com/en/1.10/topics/db/examples/many_to_many/#many-to-many-relationships

Try it like this:

article = News.objects.get(pk=pk)
person, created = Person.objects.update_or_create(first_name=fname, last_name=lname)
person.save()
person.news_mentioned.add(article)
mentioned = person.news_mentioned.all()
Jann
  • 1,799
  • 3
  • 21
  • 38
  • Yes, except that `add()` doesn't return anything, so there's no point assigning `mentioned`; it'll just be None. – Daniel Roseman Jan 13 '17 at 17:32
  • 1
    Sorry my mistake, I overlooked the variable assignment. I updated the code to show how to retrieve data from the many-to-many field. You can also use ```.filter()``` like on a normal model – Jann Jan 13 '17 at 17:35
  • Tried `.add()` as well, still not working. It saves the person, yet it doesnt create the relationship to the article. Any ideas? – PSc Jan 13 '17 at 17:51
  • I can't think of any reason why this shouldn't work. Maybe try logging the SQL statements so you can see what's happening? http://stackoverflow.com/a/20161527/246028 – Jann Jan 13 '17 at 19:59
1

I did isolate the problem and it turned out that the issue is related to update_or_create, because it returns a Tuple.

It now works like this:

            person, created = Person.objects.update_or_create(first_name=fname, last_name=lname)
            person.save()
            article = News.objects.get(pk=pk)
            person.news_mentioned.add(article)
PSc
  • 46
  • 5
  • Ah, of course! But then you should have gotten an ```AttributeError``` instead of it failing silently. – Jann Jan 15 '17 at 18:40