Django relationships and the admin page

45 views
Skip to first unread message

Liam H

unread,
Apr 10, 2017, 7:48:37 PM4/10/17
to Django users
Hi,

I'm new to Django, programming, web apps, the whole lot. I've been working on an app for a while now, but I'm having trouble figuring out how to organise my models in an effective way. Im using Django 1.10 and Python 2.7.

Here's a rough example of the structure I'm trying:

class Site(models.Model):
    #...
    def __unicode__(self):
        return self.name

class Image(models.Model):
    #...
    site = models.ForeignKey(Site, ...)
    def __unicode__(self):
        return self.name

class Location(models.Model):
    #...
    site = models.ForeignKey(Site, ...)
    def __unicode__(self):
        return self.name

class LocImage(models.Model):
    #...
    location = models.ForeignKey(Location, ...)
    def __unicode__(self):
        return self.name



The idea is to have a site (like a castle or other area the user might want to visit), and then each site will have various Images like aerial photographs associated with it. Each site will also have various locations around it with multiple images associated with that location.

So it works something like this:

Site
Images
Location
LocImages


I've tried doing it this way, but I don't know how to get the admin page to show this, for one. I read a post that said that Django's admin page can't have nested related items like this, but I'm not sure if that's actually true. I was able to run migrations on this code when I removed the references to it in the admin page, but I don't know how to access the LocImages model in the API, like I can with the Location and Images models using something like "Site.objects.get(pk=2).location_set.get(id=2).locimage_set.all()".

So, is this how I should be structuring the models? And if it works in the database is there any way to get this to show on a single page in the Django admin?

The reason I decided to structure it this way is so each location can have multiple attributes that are all fields of the same model, making it easier to link them together in the templates.

Any help would be appreciated, thanks.

Lachlan Musicman

unread,
Apr 10, 2017, 8:15:02 PM4/10/17
to django...@googlegroups.com
Maybe this is what you are looking for?
Where LocImages would be the equivalent to "Membership" in the eg?

Sounds like you are implementing the Dublin Core :)

L.

------
The most dangerous phrase in the language is, "We've always done it this way."

- Grace Hopper
 

Camilo Torres

unread,
Apr 11, 2017, 7:43:00 PM4/11/17
to Django users
Hi,
Seems you need here admin inlines, these allow you to insert/edit related objects inline when editing the main object. For example, when editing a Site, you will be able to also edit Images and Locations, though it only supports 1 level, so with default configuration you may probably not be able to also edit LocImages.
Start reading here:
http://localhost/Python/django-docs-1.10-en/ref/contrib/admin/index.html#django.contrib.admin.ModelAdmin.inlines

To support more levels, you can use django-nested-admin package:
https://pypi.python.org/pypi/django-nested-admin

Liam H

unread,
Apr 12, 2017, 2:07:09 PM4/12/17
to Django users
Yeah I was using the inlines. I actually came up with something of a workaround today. I put Location inlines in the Site page and then registered Locations on the admin page. Locations then have Image inlines. It's not as user friendly, but it will do as long as it works. I'll look into the nested admin package. Thanks for the help!
Reply all
Reply to author
Forward
0 new messages