Django: edit many, many Inline fields using Django Admin

I want to edit dishes related to a dish in the cookware panel in django admin. I am trying to use InlineModelAdmin . The problem is that I can edit DishCuisinesMap objects, but not the cuisines object. Here is a screenshot:

screenshot

These are my models.py:

class Cuisines(models.Model):
    cuisine_id = models.IntegerField(primary_key=True)
    cuisine_mtom = models.ManyToManyField('DishInfo', through='DishCuisinesMap')
    name = models.TextField()
    cuisine_sf_name = models.TextField() 
    type = models.IntegerField()
    insert_time = models.DateTimeField()
    update_time = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'cuisines'

class DishCuisinesMap(models.Model):
    dish = models.ForeignKey('DishInfo', on_delete=models.PROTECT,
                             related_name='dcm_dish')
    cuisine = models.ForeignKey(Cuisines, on_delete=models.PROTECT,
                                related_name='dcm_cuisine')
    insert_time = models.DateTimeField()
    update_time = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'dish_cuisines_map'
        unique_together = (('dish', 'cuisine'),)


class DishInfo(models.Model):
    dish_id = models.BigIntegerField(primary_key=True)
    bid = models.ForeignKey(Brands, on_delete=models.PROTECT, db_column='bid',
                            related_name='dinfo_brand')
    name = models.TextField()
    is_live = models.IntegerField()
    veg_nonveg_ind = models.SmallIntegerField(blank=True, null=True)
    sf_name = models.TextField()
    descr = models.TextField(blank=True, null=True)
    expert_tag = models.TextField(blank=True, null=True)
    special_desc = models.TextField(blank=True, null=True)
    reco_percent = models.IntegerField(blank=True, null=True)
    one_liner = models.TextField(blank=True, null=True)
    nutrition = JSONField() 
    insert_time = models.DateTimeField()
    update_time = models.DateTimeField()
    images = ArrayField(models.TextField()) 

    class Meta:
        managed = False
        db_table = 'dish_info'
        unique_together = (('bid', 'name'), ('bid', 'sf_name'),)

    def __unicode__(self):
        brand_name = self.bid.name
        return self.name

and this is my admin.py:

class CityDishFilter(admin.SimpleListFilter):
    title = ('city name')
    parameter_name = 'city_id'

    def lookups(self, request, model_admin):
        list_of_cities = list()
        queryset = Cities.objects.all()
        for city in queryset:
            list_of_cities.append(
                    (str(city.city_id), city.name)
                )
        return list_of_cities

    def queryset(self, request, queryset):
        query_params = request.GET  
        if self.value():
            city_query = DelLocations.objects.filter(city__city_id=self.value())\
                                                                        .values('del_id')
            loc_query = DelLocationsMapping.objects.filter(del_loc__in=
                                                        city_query).values('plot')
            dopm_query = DishOutletPlatMapping.objects.filter(plot__in
                                                        =loc_query).values('dish')
            final_query = DishInfo.objects.filter(dish_id__in=dopm_query)
            if 'veg_nonveg_ind' in query_params:
                final_query = final_query.filter(veg_nonveg_ind=query_params.get('veg_nonveg_ind'))

            if 'is_live' in query_params:
                final_query = final_query.filter(is_live=query_params.get('is_live'))
            return final_query
        return queryset


class BrandDishFilter(admin.SimpleListFilter):
    title = ('brand name')
    parameter_name = 'brand_id'

    def lookups(self, request, model_admin):
        query_params = request.GET
        list_of_brands = list()
        if 'city_id' in query_params:
            city_query = DelLocations.objects.filter(city__city_id=query_params.get('city_id'))\
                                                                        .values('del_id')
            loc_query = DelLocationsMapping.objects.filter(del_loc__in=
                                                        city_query).values('plot')
            dopm_query = DishOutletPlatMapping.objects.filter(plot__in
                                                        =loc_query).values('dish')
            dish_query = DishInfo.objects.filter(dish_id__in=dopm_query).\
                                                            values('bid').distinct()
            brand_query = Brands.objects.filter(bid__in=dish_query).order_by('name')
            for brand in brand_query:
                list_of_brands.append(
                        (str(brand.bid), brand.name)
                    )
            return list_of_brands
        else:
            brand_query = Brands.objects.all().order_by('name')
            for brand in brand_query:
                list_of_brands.append(
                        (str(brand.bid), brand.name)
                    )
            return list_of_brands

    def queryset(self, request, queryset):
        if 'brand_id' in request.GET:
            queryset = queryset.filter(bid=self.value())
            return queryset
        return queryset

class DishCuisinesInline(admin.TabularInline):
    model = DishCuisinesMap
    extra = 1

class CuisinesAdmin(admin.ModelAdmin):
    inlines = [DishCuisinesInline,]
    fields = ("name", "cuisine_sf_name", "type")

class DishInfoAdmin(admin.ModelAdmin):
    list_select_related = ('bid',)
    list_display = ('name', 'brand_name')
    fieldsets = (
                    (None, {'fields': ('name', 'sf_name', 'is_live', 'veg_nonveg_ind',
                                        'one_liner',
                                      )
                            }
                    ),
                    ('Advanced options', {'classes': ('collapse',),
                                           'fields': ('descr', 'images')
                                         }
                    )
                )
    search_fields = ['name', 'bid__name']
    list_filter = ('veg_nonveg_ind', 'is_live', CityDishFilter, BrandDishFilter) #, 'bid__name'
    inlines = [DishTimingsInline, DishCuisinesInline]

    # changing the size of the text fields:
    formfield_overrides = {
    models.TextField: {'widget': Textarea(
                       attrs = {'rows': 4,
                              'cols': 50})
                        },
                    }


    def brand_name(self, obj):
        return obj.bid.name


admin.site.register(DishInfo, DishInfoAdmin)
admin.site.register(Cuisines, CuisinesAdmin)

The django docs here say that membership objects can be edited on the Person or Group detail pages, but what is the way to edit a group from a person’s details page? In my case, I need a way to edit dishes from a plate, I'm pretty new to django. Any help is appreciated.

+4

Source: https://habr.com/ru/post/1657734/


All Articles