Python django模型的定制订购

Python django模型的定制订购,python,django,python-2.7,django-models,django-admin,Python,Django,Python 2.7,Django Models,Django Admin,我试图实现的是使会员名单页面上的“我的国家”列可排序。(目前不是) 问题是,我从方法country中获取“obj”作为帐户对象(如get_queryset()中定义的),然后我使用该obj获取另一个Profile类型的对象(该对象具有成员变量country)。但是,当我尝试将country方法注册到“admin_order_field”时,它只能接受obj的成员变量(Account类型,其中没有country变量) 是否有一种方法可以根据Profile类中的字段而不是Account对国家/地区列

我试图实现的是使会员名单页面上的“我的国家”列可排序。(目前不是)

问题是,我从方法country中获取“obj”作为帐户对象(如get_queryset()中定义的),然后我使用该obj获取另一个Profile类型的对象(该对象具有成员变量country)。但是,当我尝试将country方法注册到“admin_order_field”时,它只能接受obj的成员变量(Account类型,其中没有country变量)

是否有一种方法可以根据Profile类中的字段而不是Account对国家/地区列进行排序

class MemberAdmin(CustomUserAdmin, admin.ModelAdmin):
    redirect_model_name = 'memberaccountproxy'
    change_form_template = 'loginas/change_form.html'
    list_display = ('username', 'email', 'first_name', 'last_name','country', 'gender',
                    'created_at', 'profile_pic')
    search_fields = ('username', 'first_name', 'last_name', 'email',)
    add_form = AccountProfileForm
    list_filter = (
        'gender', 'incomplete', 'email_verified', 'suspended',
        'deleted',
    )        
    # profile1 = None

    def get_queryset(self, objects):
        return Account.objects.filter(usertype=1)

    def country(self, obj):
        profile1 = Profile.objects.get(account=obj)
        if profile1.country:
            return profile1.country
        return ''
    country.admin_order_field = 'country'

list\u display
admin\u order\u字段
选项支持双下划线符号来访问相关模型

您的问题不清楚,因为您尚未展示您的模型,但看起来您可以做到:

class Profile(models.Model):
    account = models.OneToOneField(Account)
    country = models.ForeignKey('Country', null=True, blank=True, verbose_name='Country Living In', related_name='+')
    # name = models.CharField(max_length=40, blank=True)
    status = models.ForeignKey('Status', blank=True, null=True, verbose_name='Marital Status', related_name='+')
    btype = models.ForeignKey('Btype', blank=True, null=True, verbose_name='Body type', related_name='+')
    bheight = models.IntegerField(choices=HEIGHT, blank=True, null=True, verbose_name='Height')
    phystatus = models.ForeignKey('EthnicOrigin', blank=True, null=True, verbose_name='Ethnic Origin', related_name='+')
    createdby = models.ForeignKey('CreatedBy', blank=True, null=True, verbose_name='Profile Created By',
                                  related_name='+')
...........................


class Account(AbstractBaseUser):
    email = models.EmailField(unique=True)
    usertype = models.IntegerField(default=1, choices=USER)
    username = models.CharField(max_length=40, unique=True)
    gender = models.CharField(choices=(('Male', 'Male'), ('Female', 'Female')), max_length=10, blank=True)
    phone = models.CharField(max_length=16, blank=True, verbose_name="Mobile No", null=True)


    temple = models.ManyToManyField('Account', blank=True, null=True, verbose_name="MatchMaker")
    first_name = models.CharField(max_length=40, blank=True, null=True)
    last_name = models.CharField(max_length=40, blank=True, null=True)
    acc_for = models.ForeignKey('CreatedBy', blank=True, null=True, verbose_name="Profile Created For")
    country_code = models.ForeignKey('CountryCode', max_length=40, blank=True, null=True)
............................................

但是,您可能可以在您的
列表显示方法中用
profile\uu country
替换
country
,并完全删除您的
country
方法。

我不确定这对您不起作用的原因。希望你能解决这个问题。
country.admin_order_field = 'profile__country'