Issue
I made a ManyToManyField in Django, but it dosen't seem like i can edit it in django admin.
After reading some documents, I found out that I needed to add some kind of widget, so I added filter_horizontal=('collections',)
(which was shown in Django Admin ManyToManyField) but this seems to cause an error.
Here are some codes I used.
# models.py
class Product(models.Model):
date_added = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=7, decimal_places=2)
digital = models.BooleanField(default=False,null=True, blank=True)
image = models.ImageField(null=True, blank=True)
description = models.CharField(max_length=1000, null=True, blank=True)
description_image = models.ImageField(null=True, blank=True)
def __str__(self):
return self.name
@property
def imageURL(self):
try:
url = self.image.url
except:
url = ''
return url
@property
def descriptionImageURL(self):
try:
url = self.description_image.url
except:
url = ''
return url
...
class Collection(models.Model):
title = models.CharField(max_length=200, null=False)
product = models.ManyToManyField(Product, related_name='collections', blank=True)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
# admin.py
from django.contrib import admin
from .models import *
admin.site.register(Customer)
admin.site.register(Product)
admin.site.register(Order)
admin.site.register(OrderItem)
admin.site.register(ShippingAddress)
admin.site.register(TextBanner)
admin.site.register(FocusSection)
class CollectionAdmin(admin.ModelAdmin):
filter_horizontal = ('collections',)
admin.site.register(Collection, CollectionAdmin)
which casues error : <class 'store.admin.CollectionAdmin'>: (admin.E019) The value of 'filter_horizontal[0]' refers to 'collections', which is not a field of 'store.Collection'.
I would really appreciate your help.
Solution
The model on which the admin acts is the Collection
model, so the field is product
:
class CollectionAdmin(admin.ModelAdmin):
filter_horizontal = ('product',)
Note: Since a
ManyToManyField
refers to a collection of elements,ManyToManyField
s are normally given a plural name. You thus might want to consider renamingtoproduct
products
.
Answered By - willeM_ Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.