Django管理员选择与默认模型排序不同的排序

时间:2021-09-08 02:29:40

I have a model (Node) which is ordered by date in the admin, so latest nodes are shown first. This is fine.

我有一个模型(节点),它在管理员中按日期排序,因此首先显示最新的节点。这可以。

The same model (Node) is referenced by another model (Device). When editing a device there is a list of nodes (in an HTML select) which is also ordered by date. I would like this select to be ordered by name and not by date.

相同的模型(节点)由另一个模型(设备)引用。编辑设备时,有一个节点列表(在HTML选择中),它也按日期排序。我希望这个选择按名称排序,而不是按日期排序。

Is it possible to have two different ordering methods, one for the list of objects and one for the select box?

是否可以使用两种不同的排序方法,一种用于对象列表,另一种用于选择框?

Thanks.

谢谢。

3 个解决方案

#1


6  

The easiest thing would be to override the formfield_for_foreignkey method in the ModelAdmin for Device, something like

最简单的方法是覆盖ModelAdmin for Device中的formfield_for_foreignkey方法,例如

def formfield_for_foreignkey(self, db_field, request, **kwargs):
    if db_field.name == 'node':
        kwargs['queryset'] = Node.objects.order_by('name')
    return super(DeviceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

(I'm assuming a fair amount here. Hopefully it's clear!)

(我在这里假设相当数量。希望很清楚!)

Similarly there's formfield_for_manytomany.

同样,还有formfield_for_manytomany。

#2


2  

Have you tried something like:

你尝试过类似的东西:

class Node
        name = ...
        date = ...
        fields ....

        class Meta:
            order_with_respect_to='Device'
            ordering = ('Device', 'name')

#3


0  

You can create custom form for DeviceAdmin and set queryset parameter for 'Node' field.

您可以为DeviceAdmin创建自定义表单,并为“Node”字段设置queryset参数。

# in models.py we have Device model with Node field
class Device(models.Model):
    node = models.ForeignKey(Node)
    ...

# in admin.py we have DeviceAdmin with custom form
class DeviceAdminForm(forms.ModelForm):
    node = forms.ModelChoiceField(queryset=Node.objects.order_by('name'), label='Node')

    class Meta:
        model = Device

class DeviceAdmin(admin.ModelAdmin):
    form = DeviceAdminForm

#1


6  

The easiest thing would be to override the formfield_for_foreignkey method in the ModelAdmin for Device, something like

最简单的方法是覆盖ModelAdmin for Device中的formfield_for_foreignkey方法,例如

def formfield_for_foreignkey(self, db_field, request, **kwargs):
    if db_field.name == 'node':
        kwargs['queryset'] = Node.objects.order_by('name')
    return super(DeviceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

(I'm assuming a fair amount here. Hopefully it's clear!)

(我在这里假设相当数量。希望很清楚!)

Similarly there's formfield_for_manytomany.

同样,还有formfield_for_manytomany。

#2


2  

Have you tried something like:

你尝试过类似的东西:

class Node
        name = ...
        date = ...
        fields ....

        class Meta:
            order_with_respect_to='Device'
            ordering = ('Device', 'name')

#3


0  

You can create custom form for DeviceAdmin and set queryset parameter for 'Node' field.

您可以为DeviceAdmin创建自定义表单,并为“Node”字段设置queryset参数。

# in models.py we have Device model with Node field
class Device(models.Model):
    node = models.ForeignKey(Node)
    ...

# in admin.py we have DeviceAdmin with custom form
class DeviceAdminForm(forms.ModelForm):
    node = forms.ModelChoiceField(queryset=Node.objects.order_by('name'), label='Node')

    class Meta:
        model = Device

class DeviceAdmin(admin.ModelAdmin):
    form = DeviceAdminForm