Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I reference Django Model from another model
    primarykey
    data
    text
    <p>Im looking to create a view in the admin panel for a test program which logs Books, publishers and authors (as on djangoproject.com)</p> <p>I have the following two models defined.</p> <pre><code>class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) email = models.EmailField() def __unicode__(self): return u'%s %s' % (self.first_name, self.last_name) class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField() def __unicode__(self): return self.title </code></pre> <p>What I want to do, is change the Book model to reference the first_name of any authors and show this using admin.AdminModels. </p> <pre><code>#Here is the admin model I've created. class BookAdmin(admin.ModelAdmin): list_display = ('title', 'publisher', 'publication_date') # Author would in here list_filter = ('publication_date',) date_hierarchy = 'publication_date' ordering = ('-publication_date',) fields = ('title', 'authors', 'publisher', 'publication_date') filter_horizontal = ('authors',) raw_id_fields = ('publisher',) </code></pre> <p>As I understand it, you cannot have two ForeignKeys in the same model. Can anyone give me an example of how to do this?</p> <p>I've tried loads of different things and its been driving me mad all day. Im pretty new to Python/Django.</p> <p>Just to be clear - I'd simply like the Author(s) First/Last name to appear alongside the book title and publisher name. </p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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