在Django中引用扩展的多用户模型...?

时间:2017-06-15 16:18:48

标签: django django-models django-forms django-templates django-views

我在模板中引用了扩展用户模型(UserProfile)时遇到了一些问题。

我所设置的是当前登录的用户将是"用户对象"与该评论相关联。我这样做了如下:

admin.py:

class CommentAdmin(admin.ModelAdmin):
    list_display = ('created_by', 'post', 'created', 'active')
    list_filter = ('active', 'created')
    search_fields = ('created_by', 'body')

    def save_model(self, request, obj, form, change):
        instance = form.save(commit=False)
        if not hasattr(instance,'created_by'):
            instance.created_by = request.user
        instance.save()
        form.save_m2m()
        return instance

    def save_formset(self, request, form, formset, change):

        def set_user(instance):
            if not instance.created_by:
                instance.created_by = request.user
            instance.save()

        if formset.model == Comment:
            instances = formset.save(commit=False)
            map(set_user, instances)
            formset.save_m2m()
            return instances
        else:
            return formset.save()

我的评论模型如下:

class Comment(models.Model):
    created_by = models.OneToOneField(User, related_name='created_by', null=True, blank=True)
    post = models.ForeignKey(Post, related_name='comments')
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=True)

    class Meta:
        ordering = ('created',)

    def __str__(self):
         return "Comment by {} on {}".format(self.created_by, self.post)

我的UserProfile模型扩展了User模型,如下所示:

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='userprofile')
    user_avatar = models.ImageField(storage=site_media_upload_location, null=True, blank=True)

一切正常。我可以保存注释并将所有注释关联到登录的用户实例。我现在的问题是:我想为与每个评论相关联的用户显示user_avatar(在UserProfile模型中定义)(而不是当前登录的用户)。

到目前为止,我已尝试过以下内容:

<img src="{{ MEDIA_URL }}{{ comment.created_by.user.userprofile.user_avatar.url }}">

<img src="{{ MEDIA_URL }}{{ comment.created_by.userprofile.user_avatar.url }}">

但似乎没有任何效果(最后的猜测更多的是绝望而非实际的可能性)。

因此,从上面的模型 - 以及使用当前记录的User对象的实例创建注释的知识 - 我如何才能使这一切工作正常?

我唯一的另一个想法是,我可能需要在评论模型中添加类似的内容:

class Comment(models.Model):
    commenter_avatar = models.OneToOneField(UserProfile.user_avatar, related_name='user_avatar', null=True, blank=True)

也许我可以在评论admin save()方法中请求user.userprofile.user_avatar?

2 个答案:

答案 0 :(得分:0)

图片字段的 boolean flag = false; while(rs.next()){ boolean type=rs.getBoolean("Administrator"); String dbUser=rs.getString("UserName"); String dbPass=rs.getString("Password"); if(un.equals(dbUser)&& pw.equals(dbPass)){ if(type == true){ AdMenu admenu=new AdMenu(); admenu.show(); this.setVisible(false); } else{ Menu menu=new Menu(); menu.show(); this.setVisible(false); } flag = true; break; } } if(flag == true){ JOptionPane.showMessageDialog(null,"Ïncorrect username or password"); } 属性已添加url,请参阅https://docs.djangoproject.com/en/1.11/topics/files/#using-files-in-models

所以最终的标签应该是:

MEDIA_URL

如果用户个人资料实际上有图像

答案 1 :(得分:0)

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='userprofile')
    user_avatar = models.ImageField(upload_to="images/") // no need null=True, blank=True

class Comment(models.Model):
    created_by = models.ForeignKey(UserProfile, related_name='created_by', null=True, blank=True)
    post = models.ForeignKey(Post, related_name='comments')
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=True)

    class Meta:
        ordering = ('created',)

    def __str__(self):
         return "Comment by {} on {}".format(self.created_by, self.post)


<img src="{{ MEDIA_URL }}{{ comment.created_by.user_avatar.url }}">

我认为你使用太多的USER关系来简化你的代码。 试试!!