Python 基于内容类型和字段名链接识别Django用户表

Python 基于内容类型和字段名链接识别Django用户表,python,django,django-contenttypes,Python,Django,Django Contenttypes,我正在编写一个管理订阅的应用程序,例如年度俱乐部会员。有不同的成员类别,这些类别可以具有管理员指定的标准,这些标准可以与数据库中的任何表相关。唯一的要求是必须有一个指向Django用户表的链接 因此,我得到了这个模型(定义如下),其中: 名称是为方便用户而使用的类别名称 content\u type是指向django\u content\u type表的链接,用于识别 正在为此类别设置标准的表格 过滤条件是用户选择相关用户的条件 (例如,如果content\u type表是User,那么这可能

我正在编写一个管理订阅的应用程序,例如年度俱乐部会员。有不同的成员类别,这些类别可以具有管理员指定的标准,这些标准可以与数据库中的任何表相关。唯一的要求是必须有一个指向Django用户表的链接

因此,我得到了这个模型(定义如下),其中:

  • 名称是为方便用户而使用的类别名称
  • content\u type是指向
    django\u content\u type
    表的链接,用于识别 正在为此类别设置标准的表格
  • 过滤条件是用户选择相关用户的条件 (例如,如果
    content\u type
    表是User,那么这可能是 就像
    {“is_active”:“true”}
  • user\u link是由
    content\u type
    这是用户表的外键
我想在管理员保存时检查
user\u链接
does链接到用户表,为此我正在编写
def clean(self)

我无法确定如何将我的
内容\u类型
用户\u链接
字段转换为一个对象,我可以用来检查它是否是用户表。
非常欢迎帮助

这是models.py

from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
import datetime

from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

from django.contrib.contenttypes.models import ContentType
from subscriptions.fields import JSONField

class Category(models.Model):
    name = models.CharField('Category', max_length=30)
    content_type = models.ForeignKey(ContentType)
    filter_condition = JSONField(default="{}", help_text=_(u"Django ORM compatible lookup kwargs which are used to get the list of objects."))
    user_link = models.CharField(_(u"Link to User table"), max_length=64, help_text=_(u"Name of the model field which links to the User table.  'No-link' means this is the User table."), default="No-link")

    def clean (self):
        if self.user_link == "No-link":
            if self.content_type.app_label == "auth" and self.content_type.model == "user":
                pass
            else:
                raise ValidationError(
                    _("Must specify the field that links to the user table.")
                    )
        else:
            t = getattr(self.content_type, self.user_link)
            ct = ContentType.objects.get_for_model(t)
            if not ct.model == "user":
                raise ValidationError(
                    _("Must specify the field that links to the user table.")
                    )    



    def __unicode__(self):
        return self.name

    def _get_filter(self):
        # simplejson likes to put unicode objects as dictionary keys
        # but keyword arguments must be str type
        fc = {}
        for k,v in self.filter_condition.iteritems():
            fc.update({str(k): v})
        return fc

    def object_list(self):
        return self.content_type.model_class()._default_manager.filter(**self._get_filter())

    def object_count(self):
        return self.object_list().count()

    class Meta:
        verbose_name = _("Category")
        verbose_name_plural = _("Categories")
        ordering = ('name',)

不同的描述可以有不同的标准。

出于验证目的,我得到了下面的解决方案,它取代了
def clean
中的主
else
子句

    else:
        s = apps.get_model(self.content_type.app_label, self.content_type.model)
        if not hasattr(s, self.user_link):
            raise ValidationError(
                _("Must specify the field that links to the user table.")
                )

我现在需要弄清楚如何实际使用这些信息,并将两者连接到一个字段中,这样我就可以链接到
用户

应用程序来自哪里?