gpt4 book ai didi

python - 使用 Django 定义 ManyToManyField 的顺序

转载 作者:IT老高 更新时间:2023-10-28 22:03:50 28 4
gpt4 key购买 nike

有没有办法通过 ManyToManyField 定义相关对象的顺序?

例子:

  • ArticleContainer1 按此顺序包含:
    第1条、第2条、第3条、第6条

  • ArticleContainer2 按此顺序包含:
    第 3 条、第 2 条、第 1 条、第 4 条

  • ArticleContainer3 按此顺序包含:
    第五条

这是我的类(class):

class Article(models.Model):    
title = models.CharField(max_length=200)

class ArticleContainer(models.Model):
contents = models.ManyToManyField(Article, blank=True, null=True)

最佳答案

这是我的一个示例,一个将人员组织到部门中并按部门排序的网站。它与您的问题的概念相同,但模型不同。本例使用多对多直通表。

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

以及直通模型:

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,但对于这个例子来说它不必要的复杂。你可以通过一个简单的

class PersonAdmin(admin.ModelAdmin) :
pass

但这是我在我的应用程序中使用的:

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)

关于python - 使用 Django 定义 ManyToManyField 的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9602217/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com