Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango no entry in many to many table
    primarykey
    data
    text
    <p>I have a problem with django that I really cant get my head around. Been stuck on it for two days and been looking in the source but I cant find any explanation, hopefully someone knows this better than I do!</p> <p>I have two models, <code>Publication</code> and <code>Article</code> like this</p> <pre><code>class Publication(models.Model): title = models.CharField(max_length=30) articles = models.ManyToManyField('Article',blank=True) class Meta: db_table = 'publications' class Article(models.Model): headline = models.CharField(max_length=100) class Meta: db_table = 'articles' </code></pre> <p>When I save a new <code>Publication</code> I want to automatically save a default article. I did a script to test this and it works fine. Both objects are created in their respective tables and one entry is written to the <code>publications_articles</code> table. As expected.</p> <pre><code>publication = Publication(title='The new exciting publication') publication.save() publication.articles.create(headline='The article that makes the pub so exciting') </code></pre> <p>However, here is the tricky part. To have the same functionality when saving a <code>Publication</code> in the admin I did override the <code>save_model()</code> function like this.</p> <pre><code>class PublicationAdmin(admin.ModelAdmin): list_display = ['title'] def save_model(self, request, obj, form, change): if not change: obj.save() obj.articles.create(headline='The article that makes this pub so exciting') else: obj.save() </code></pre> <p>Saving a <code>Publication</code> in the admin creates both the <code>Publication</code> and the <code>Article</code> but theres no entry in the <code>publications_articles</code> table tying them together! </p> <p>Someone please, please explain this behavior. I'm about to do a raw sql-insert just to get through with it but I want to understand why the connection between the entities is not made.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload