Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango adding a ManyToMany field/table to existing schema, related_name error
    text
    copied!<p>I have an existing project with models (Users and Books). I would like to add a ManyToMany(M2M) field to the existing model Books, but the syncbb command does not do this.</p> <p>Details: Books already has a FK field that maps to User, and I want to add a new M2M field (readers) that also maps to User. As you know, Django's syncdb only cares about tables, so adding a normal field is easy, but a M2M requires a new join table (app_books_user), so shouldn't syncdb cmd add this like any other new table? It created my other joined table for the Book's 'sellers' field.</p> <p>When I ran syncdb, I initially received an error instructing me to use the 'related_name' argument to help differentiate the two references to User. I added those. However, when I run syncdb again, it does not create the new join table (but it's now error-free). The new field exists when I view it via the Shell, but can't use it b/c the join table doesn't exist. I looked at the sql code via 'sqlall' cmd and it prints out the SQL for the new table, but it doesn't get executed.</p> <p>What am I missing? Should I just force the SQL (from the sqlall) via my database browser? Will that have any repercussions? Code follows:</p> <p>Models.py</p> <pre><code>from django.contrib.auth.models import User class Seller(models.Model): ... class Books(models.Model): name=models.CharField(max_length=50) author=models.ForeignKey(User, related_name='creator') readers=models.ManyToManyField(User, blank=True, related_name='viewers') sellers=models.ManyToManyField(Seller) </code></pre> <p>Thank you</p>
 

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