引用User的ForeignKey和ManyToManyFiel

引用User的ForeignKey和ManyToManyFiel

本文介绍了Django Admin:引用User的ForeignKey和ManyToManyField关系的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Django的 UserProfile 来扩展内置的Django User 模型的应用程序。看起来有点像:

  class UserProfile(models.Model):
user = models.ForeignKey(用户,唯一= true)
#Local Stuff
image_url_s = models.CharField(max_length = 128,blank = True)
image_url_m = models.CharField(max_length = 128,blank = True)

#Admin
class Admin:pass

我已经添加了一个新类我的模型:

  class Team(models.Model):
name = models.CharField(max_length = 128)
manager = models.ForeignKey(User,related_name ='manager')
members = models.ManyToManyField(User,blank = True)

它被注册到管理员:

  class TeamAdmin(admin.ModelAdmin )
list_display =('name','manager')

admin.site.register(Team,TeamAdmin)

嗯,在管理界面中,当我从下拉框中选择一个经理,或者se通过多选字段,小组成员按照用户数字ID进行排序。对于我的生活,我不知道如何得到这些排序。



我有一个类似的课程:

  class Meta:
ordering = ['name']

这很棒!但是我不会拥有用户类,当我在 UserAdmin 中尝试这个技巧时:

  class Meta:
ordering = ['username']

我得到:



django.core.management.base.CommandError:一个或更多的模型没有验证:
events.userprofile:排序是指username,不存在的字段。



user.username 也不起作用。我可以指定,如 image_url_s 如果我想。 。 。如何告诉管理员通过 username 对用户列表进行排序?谢谢!

解决方案

一种方法是在管理员中定义一个用于Team模型的自定义表单,并覆盖 manager 字段使用具有正确排序的查询器:

  from django import 

class TeamForm(forms.ModelForm):
manager = forms.ModelChoiceField(queryset = User.objects.order_by('username'))

class Meta :
model = Team

class TeamAdmin(admin.ModelAdmin):
list_display =('name','manager')
form = TeamForm


I have an application that makes use of Django's UserProfile to extend the built-in Django User model. Looks a bit like:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    # Local Stuff
    image_url_s = models.CharField(max_length=128, blank=True)
    image_url_m = models.CharField(max_length=128, blank=True)

    # Admin
    class Admin: pass

I have added a new class to my model:

class Team(models.Model):
    name = models.CharField(max_length=128)
    manager = models.ForeignKey(User, related_name='manager')
    members = models.ManyToManyField(User, blank=True)

And it is registered into the Admin:

class TeamAdmin(admin.ModelAdmin):
    list_display = ('name', 'manager')

admin.site.register(Team, TeamAdmin)

Alas, in the admin inteface, when I go to select a manager from the drop-down box, or set team members via the multi-select field, they are ordered by the User numeric ID. For the life of me, I can not figure out how to get these sorted.

I have a similar class with:

class Meta:
    ordering = ['name']

That works great! But I don't "own" the User class, and when I try this trick in UserAdmin:

class Meta:
    ordering = ['username']

I get:

django.core.management.base.CommandError: One or more models did not validate:events.userprofile: "ordering" refers to "username", a field that doesn't exist.

user.username doesn't work either. I could specify, like image_url_s if I wanted to . . . how can I tell the admin to sort my lists of users by username? Thanks!

解决方案

One way would be to define a custom form to use for your Team model in the admin, and override the manager field to use a queryset with the correct ordering:

from django import forms

class TeamForm(forms.ModelForm):
    manager = forms.ModelChoiceField(queryset=User.objects.order_by('username'))

    class Meta:
        model = Team

class TeamAdmin(admin.ModelAdmin):
    list_display = ('name', 'manager')
    form = TeamForm

这篇关于Django Admin:引用User的ForeignKey和ManyToManyField关系的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:54