Django: ManyToMany Inline Admin view error
Here are the model definitions:
class ItemBrand(models.Model):
name = models.CharField(max_length = 30, unique = True)
def __unicode__(self):
return self.name
class WantedItem(models.Model):
name = models.CharField(max_length = 120)
description = models.TextField()
created = models.DateTimeField(auto_now = False, auto_now_add = True)
expires = models.DateTimeField(auto_now = False, auto_now_add = False)
type = models.ForeignKey(ItemType, related_name = "type wanted")
GENDER_CHOICES = (
(1, 'Male'),
(2, 'Female')
)
gender = models.IntegerField(choices = GENDER_CHOICES)
brands = models.ManyToManyField(ItemBrand, related_name = "wantedbrands", symmetrical = False)
colors = models.ManyToManyField(ItemColor)
sizes = models.ManyToManyField(ItemSize)
creator = models.ForeignKey(User, related_name = "wishlist creator")
def __unicode__(self):
return self.name
Here is the AdminModel code:
class BrandsInline(admin.TabularInline):
model = WantedItem.brands.through
class WantedItemAdmin(admin.ModelAdmin):
list_display = ('name', 'created', 'expires', 'type', 'gender', 'creator')
search_fields = ('name', 'description')
list_filter = ('created', 'brands',)
ordering = ('-created',)
inlines = [
BrandsInline,
]
exclude = ('brands',)
This is pulled basically right from the Django docs, and here's the error I am getting:
'ReverseManyRelatedObjectsDescriptor' object has no attribute 'through'
I am at a total loss... any ideas? Even if I literally create a linker table and set the "through" attribute in the Model I get the开发者_开发知识库 same error.
Broken?
You need to upgrade Django to the trunk.
Using inlines with many-to-many fields is new in the django development version (see docs).
Using a simplified version of your models, I get the same error as you for Django 1.1.1, but it works on the trunk (revision 11785).
As an aside, you don't need to specify symmetrical = False
on your ItemBrand
ManyToMany field. The symmetrical option is only intended for recursive relationships eg User <-> User
.
You may want to have a look at the documentation on related names, and think about renaming them to something more logical as well. If creator
is a User
object, and want to get the set of wishlists they have created, the default when related_name
is not specified is
creator.wishlist_set.all()
with your choice for related_name
(when you add the underscore), this changes to
creator.wishlist_creator.all()
but I would recommend related_name='wishlists'
, in which case you would use
creator.wishlists.all()
While it may not be the cause of your error, spaces in the related_name
attribute are invalid so I'd try removing those first.
"type wanted" => "type_wanted"
"wishlist creator" => "wishlist_creator"
精彩评论