Django - 如何为注册网站用户和非网站用户提供模型?

时间:2012-11-17 10:43:27

标签: django django-admin django-authentication

我有一个Trip模型,可以让许多参与者订阅特定旅行和一个所有者。参与者是在网站上注册的用户,但我也希望能够在旅行中添加“离线”用户,在网站上没有帐户的用户,以便我可以跟踪所有用户。 所有者和参与者链接到Userena用户配置文件(附带问题:可能直接链接到User会更好吗?但是如何让full_name在内联管理员中看到选择呢?)

class Trip(models.Model):
    name = models.CharField('trip name', max_length=200)
    type = models.ForeignKey(Category, related_name='categories')
    .
    .
    .
    slug = models.SlugField('trip slug', max_length=100)
    owner = models.ForeignKey(UserProfile, related_name='owner')
    participants = models.ManyToManyField(UserProfile, blank=True, null=True, related_name='participants')
    is_public = models.BooleanField("is visible?",db_index=True)

class ParticipationInline(admin.TabularInline):
    model = Trip.participants.through
    extra = 3

class TripAdmin(admin.ModelAdmin):

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name in ('desc',):
            return db_field.formfield(widget=TinyMCE(
                attrs={'cols': 100, 'rows': 20},
                mce_attrs={'external_link_list_url': reverse('tinymce.views.flatpages_link_list')},
            ))
        return super(TripAdmin, self).formfield_for_dbfield(db_field, **kwargs)
    inlines = [
        ParticipationInline,
        ]
    exclude = ('participants',)
    prepopulated_fields = {"slug": ("name",)}
    list_display = ('name', 'type', 'date', 'dest','owner', 'is_public')
    list_filter = ['date', 'type']
    search_fields = ['name', 'dest', 'desc']
    date_hierarchy = 'date'

    class Media:
        js = ('js/tiny_mce/tiny_mce.js',
              'js/tiny_mce/textareas.js',)


admin.site.register(Trip, TripAdmin)

因此,当我通过管理界面添加Trip时,我想首先从现有注册用户中选择一个参与者,如果我在那里找不到它们,我希望能够内联添加新的'离线'参与者。实现这一目标的最佳方法是什么?

  1. 从我的UserProfile继承两个配置文件?
  2. Trip中为离线用户和两个ManyToMany关系设置单独的模型?
  3. 有更好的方法吗?
  4. 在Hedde的评论之后

    编辑

    我按照GenericRelation的建议创建OfflineUserTrip

    class OfflineUser(models.Model):
        first_name = models.CharField(_('first'), max_length=30)
        last_name = models.CharField(_('last'), max_length=30)
        ...
        content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        content_object = generic.GenericForeignKey("content_type", "object_id")    
    
    class Trip(models.Model):
        ...
        owner = models.ForeignKey(UserProfile, related_name='owner')
        participants = models.ManyToManyField(UserProfile, blank=True, null=True, related_name='participants')
        offline_users = generic.GenericRelation(OfflineUser)
    

    并在添加OfflineUserInline后,我可以将注册用户和离线用户添加到Trip! 然后,我可以列出这两种类型的Trip参与者:

    {% if request.user.is_staff %}
            <ul>
                {% for participants in object.participants.all %}
                    <li> {{ participants.full_name }}
                    </li>
                {% empty %}
                    No registered users!
                {% endfor %}
                {% for participants in object.offline_users.all %}
                    <li> {{ participants.full_name }}
                    </li>
                {% empty %}
                    No offline users!
                {% endfor %}
            </ul>
    {% endif %}
    

    所以它起到了一定的作用,现在我有一种奇怪的感觉,我并没有完全理解Hedde的答案......

    每次我想与参与者做一些事情(计算,列出等等)我需要做两次吗?

1 个答案:

答案 0 :(得分:1)

使用contenttypes framework可以让这更容易。您可以从管理员过滤/隐藏内容类型并清理表单或在需要时覆盖保存。对于“离线”参与者,我不会创建个人资料,但会模仿,例如:

class OfflineUser(models.Model):
    first_name = models.CharField(_('first name'), max_length=30)
    last_name = models.CharField(_('last name'), max_length=30)
    email = models.EmailField(_('e-mail address'))
    # field extensions
    ... = generic.GenericRelation(...)

    def get_profile(self):
        pass

    def get_full_name(self):
        """
        Add some default User methods so you can call them on
        the related_object without raising attribute errors
        """
        full_name = u'%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

现在你可以做以下事情:

{{ some_model.content_object.get_profile.get_full_name }}

有很多stackoverflow和谷歌搜索结果可以帮助你,例如

Generic many-to-many relationships