使用django定义ManyToManyField的顺序 [英] Define an order for ManyToManyField with django
问题描述
我有一种方法来定义内容列表项的顺序?
I there a way to define the order of the contents List items?
例如:
-
ArticleContainer1包含以下顺序:
article1,article2,article3,article6
ArticleContainer1 contains in this order :
article1, article2, article3, article6
ArticleContainer2包含此订单:
article3,article2,article1,article4
ArticleContainer2 contains in this order :
article3, article2, article1, article4
ArticleContainer3按以下顺序包含:
article5
ArticleContainer3 contains in this order :
article5
这是我的课程:
class Article(models.Model):
title = models.CharField(max_length=200)
class ArticleContainer(models.Model):
contents = models.ManyToManyField(Article, blank=True, null=True)
推荐答案
p>所以这是一个例子,我有一个网站,将人们组织到部门的部门订购。它与您的问题相同,但具有不同的型号。此示例使用多对多通过表。
So this is an example I have, a site that organizes people into departments with per department ordering. Its the same concept as your problem but with different models. This example uses many-to-many through table.
class Department(models.Model):
slug = models.SlugField(
verbose_name = _(u'Slug'),
help_text = _(u'Uri identifier for this department.'),
max_length=255
)
name = models.CharField(
verbose_name = _(u'Department Name'),
help_text = _(u'The department\'s name.'),
max_length = 255
)
description = models.TextField(
verbose_name = _(u'Description'),
help_text = _(u'Department\'s description')
)
people = models.ManyToManyField(
Person,
through = 'DepartmentPeople',
related_name = 'people',
verbose_name = _(u'People'),
help_text = _(u'People in this Department')
)
order_by = models.IntegerField(
verbose_name = _(u'Ordering Weight'),
help_text = _(u'This item\'s weight within a list.'),
max_length = 255
)
class Meta:
verbose_name = _(u"Department")
verbose_name_plural = _(u"Departments")
ordering = ['order_by',]
def people_list(self):
return [dp.person for dp in DepartmentPeople.objects.filter(department=self).order_by('order')]
def __unicode__(self):
return self.name
直通模型:
And the through model:
class DepartmentPeople(models.Model):
person = models.ForeignKey(
Person,
verbose_name = _(u'Person'),
help_text = _(u'Person is a member of this deparment.'),
)
department = models.ForeignKey(
Department,
verbose_name = _(u'Department'),
help_text = _(u'Pseron is a member of this department.'),
)
order = models.IntegerField(
verbose_name = _(u'Order'),
help_text = _(u'What order to display this person within the department.'),
max_length = 255
)
class Meta:
verbose_name = _(u"Department Person")
verbose_name_plural = _(u"Department People")
ordering = ['order',]
def __unicode__(self):
return self.person.first_name + " " + self.person.last_name + " is a member of " + self.department.name + (" in position %d" % self.order)
而管理员:
class DepartmentPeopleInline(admin.TabularInline):
model = DepartmentPeople
extra = 1
class DepartmentAdmin(admin.ModelAdmin):
inlines = (DepartmentPeopleInline,)
admin.site.register(Person, PersonAdmin)
admin.site.register(Department, DepartmentAdmin)
请求评论:
注意:以下是我的PersonAdmin ,但这个例子不必要地复杂了。你可以通过一个简单的
REQUEST IN COMMENT:
Note: the following is my PersonAdmin, but its needlessly complicated for this example. you could get by with a simple
class PersonAdmin(admin.ModelAdmin) :
pass
但是这是我在我的应用程序中使用的:
BUT this is what i'm using in my app:
class PersonForm(forms.ModelForm):
abstract = forms.CharField(
widget=TinyMCE(attrs={'cols': 80, 'rows': 30})
)
class Meta:
model = Person
class PersonAdmin(reversion.VersionAdmin):
form = PersonForm
# The Form Fieldsets
fieldsets = [
(
None,
{
'fields' : [('first_name', 'last_name', 'post_nominal', ), 'slug', 'title', 'headshot', 'large_photo', ('email', 'phone', ), 'abstract']
},
)
]
# Prepopulated fields
prepopulated_fields = {'slug': ('first_name', 'last_name', 'post_nominal', )}
# Fields that are readonly
#readonly_fields = ('slug', )
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'headshot':
request = kwargs.pop("request", None)
kwargs['widget'] = AdminImageWidget
return db_field.formfield(**kwargs)
return super(PersonAdmin, self).formfield_for_dbfield(db_field, **kwargs)
这篇关于使用django定义ManyToManyField的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!