简体   繁体   中英

Many to Many and Foreign Key relations in django admin

First my django knowledge is beginner level, so please be patient with me. I am faced with a model relationship that I do not know how to handle. I have 3 models: Project, Location and SubLocation.

A project can have multiple locations and each location can have many sublocations. I have a many to many field for location in the Project model and a foreignkey field for location in the Sublocation model.

class Project(models.Model):
    ...
    locations = models.ManyToManyField(Location)

class Location(models.Model):
    name = models.CharField(max_length=250, unique=True)

class SubLocation(models.Model):
    location = models.ForeignKey(Location)
    name = models.CharField(max_length=100, unique=True)

In django admin, I am able to add multiple locations when creating a project(Using filter_horizontal). However, I also need the option to select sublocations based on an added location for the project being created. I did not know how to do it with the above approach.

I then removed the locations many to many field from the project model tried the approach below.

I created a ProjectLocation model and added it as an inline to the Project ModelAdmin to be able to add locations and sublocations when creating a project. The model that looks as follows:

class ProjectLocation(models.Model):
    project = models.ForeignKey(Project)
    location = models.ManyToManyField(Location)
    sublocations = models.ManyToManyField(SubLocation)

However, the approach does not work as desired since you can add any sublocations irregardless of the locations added. What I would like is to be able to add locations and their relevant sublocations when creating a project.

I read through generic relations as another possible approach but still I did not know how to pull it off.

  1. With my model structure, is that possible?
  2. If True, what should I do to get the desired result?
  3. If False, please recommend how I could change it.

我认为,如果您使用外键,这将使您的情况变得更容易,并且可以与django的_set选项一起使用。

  1. Yes it is possible but it will not be dynamic (meaning that changing the value of location will not magically update themselves without first saving the change) which might be very unpractical.
  2. You should use formfield_for_manytomany in your admin -> https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_manytomany
  3. The easiest way to implement this would be to add custom javascript filtering on that particular admin form

Leave for models as it was and try to use inlines in admin page.

So, your admins.py would look something like this:

class SubLocationInline(admin.StackedInline):
    model = SubLocation

@admin.register(Location)
class LocationAdmin(admin.ModelAdmin):
    .....   
    inlines = [SubLocationInline]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM