【问题标题】:how to get access to the fields inherited models?如何访问继承模型的字段?
【发布时间】:2014-10-11 16:37:39
【问题描述】:

请帮助解决问题。

我有一个模型:

class UserProfile(User):            
    gender = models.ForeignKey(
        Gender,
        verbose_name='Пол',
        blank=True,
        null=True,
    )
    nickname = models.CharField(
        'Отображаемое имя',
        max_length=50, 
        blank=False,
    )   
    phone = models.CharField(
        max_length=50, 
        blank=False,
    )
    skype = models.CharField(
        max_length=50, 
        blank=False,
    )   
    other = models.TextField(
        max_length=500,
        blank=False,
    )
    avatar = models.ImageField(
        upload_to='userprofile/avatar/', 
        blank=True,
        null=True,
    )

    objects = UserManager()

    @classmethod
    def get_new_authors_entries(self, cut_begin=0, cut_end=2):
        return self.objects.filter(is_active=1, is_superuser=0)[cut_begin:cut_end]  

get_new_authors_entries() 方法返回以下字段:

[
    {
        "fields": 
            {
                "skype": "dfsdf", 
                "user_permissions": [], 
                "other": "zzzz", 
                "nickname": "\u0418\u0432\u0430\u043d \u0422\u0443\u0440\u0433\u0435\u043d\u0435\u0432 \u0451\u043f\u0442\u0430", 
                "gender": 1, 
                "avatar": "", 
                "phone": "2014-0921", 
                "groups": []
            }, 

        "model": "app_accounts.userprofile", 
        "pk": 4
    }, 

    .....,
    .....,
    ....
] 

但我需要获取字段的值:'date_joined'、'username'、'email'

从用户继承的用户配置文件。而且我看不出为什么我无法访问以下字段:'date_joined'、'username'、'email'

views.py(片段):

new_authors = UserProfile.get_new_authors_entries(cut_begin=page_new_authors, cut_end=page_new_authors + 2)

result = serializers.serialize('json', new_authors)

return HttpResponse(json.dumps(result), content_type='application/json')    

【问题讨论】:

    标签: python django python-3.x


    【解决方案1】:

    我建议不要从用户继承,而是将配置文件设为自己的模型,该模型具有对用户的外键引用。无论哪种方式,您没有看到任何用户模型字段的原因是因为默认情况下序列化程序不执行“深度序列化”。您必须实现一个可适当序列化的自定义序列化程序。

    如果您想从哪里开始,请查看 Tom Christie 的旧解决方案:https://github.com/tomchristie/django-serializers

    如今,大部分内容都包含在 django-rest-framework 中。恕我直言,对于简单的用例,使用标准 json 模块滚动您自己的序列化程序并不算太多工作。它还使您可以完全控制生成的 json 的结构,这很好。最新版本的 Django 甚至内置了 JsonResponse,因此您可以执行以下操作:

        #at the end of your view
        import json
        response_data = json.dumps(serialize_data(user,profile))
        return JsonResponse(response_data, status=200)
    
    def serialize_data(user, profile):
    
        my_dict = {
            'username': user.username,
            'date_joined': user.date_joined
            #... and so on
        }
    
        return my_dict
    

    【讨论】:

      【解决方案2】:

      也许因为这是一个类方法,所以它应该接收 cls 并在里面使用 cls 而不是 self。

      【讨论】:

        猜你喜欢
        • 2012-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-16
        • 1970-01-01
        • 1970-01-01
        • 2019-10-22
        相关资源
        最近更新 更多