Django与用户默认身份验证模型和另一个模型的关系 [英] Django relationship with User default auth model and another Model

查看:52
本文介绍了Django与用户默认身份验证模型和另一个模型的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,用户可以在其中订阅系统内部可以看到的不同系列的文本.我想在 User SeriesText 之间创建一种关系,这样我就可以在VIEW中获取当前登录的用户,并只需在仪表板模板处返回其所有订阅即可..用户将可以订阅任意数量的剧集.

I’m developing a web application where users can subscribe to different series of texts, that would be seen inside the system. I want to create a relationship between the User and the SeriesText, so I can grab the current logged user in the VIEW, and just return all of his subscriptions at the dashboard template. An user would be able to subscribe to as many series as he wants.

我正在使用 django.contrib.auth.models 中的默认 User 模型,并且不确定如何创建这种关系.

I’m using the default User model from django.contrib.auth.models, and I’m not sure how to create this relationship.

我读了很多书,我认为这里的正确用法是多对多(是正确的吗?),所以我尝试使用称为的数据透视表/模型进行此操作订阅:

I read a lot and I think the correct usage here would be Many-to-many (is that correct?), so I tried this, using a pivot table/model called Subscriptions:

from django.contrib.auth.models import User as BaseUserClass

class User(BaseUserClass):
    subscriptions = models.ManyToManyField(SeriesText, through="Subscription")


 class SeriesText(models.Model):
    series_name = models.CharField(max_length=255)
    series_text = models.CharField(max_length=255)
    subbed_users = models.ManyToManyField(User, through="Subscription")


class Subscription(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    series = models.ForeignKey(SeriesText, on_delete=models.CASCADE)

    def subscribe(self, user_id, series_id):
        self.user = user_id
        self.series = series_id
        self.save()

但是这似乎没有用,即使尝试使用 User.objects.get(pk = 1),我也遇到了错误,真的不知道为什么.

But that didn’t seem to work, I got errors even trying to use a User.objects.get(pk=1), don’t really know why.

我真的很困惑,如果我需要双向关系,例如在 SeriesText 模型和扩展的 User <上创建的 models.ManyToMany /code>模型(我什至不知道这是否真的是这样做的方式).我什至不确定使用默认的auth用户模型建立关系的正确方法.

I’m really confused if I need to put the relationship both ways, like created models.ManyToMany on SeriesText model, and on an extended User model (that I don’t even know if that’s really the way to do it). I'm not even sure if that the correct way to make a relationship using the default auth user model.

为了以后能够搜索所有订阅该系列的用户,我认为 models.ManyToMany 也应该在 SeriesText 模型上,这也是对吗?

To be able to later also search for all users subscribed to a series, I think that the models.ManyToMany should also be on the SeriesText model, is that also correct?

有人可以帮助我了解我是否使用了正确的关系(多对多),以及如何建立这种关系?预先感谢,我是Django的新手,我在这里碰墙.

Can someone help me understand if I’m using the correct relationship (many-to-many), and how to make this relationship? Thanks in advance, I’m pretty new to Django I’m hitting a wall here.

推荐答案

您只需要在 SeriesText 上添加一个m2m字段.从根本上讲,多对多字段附加到哪个模型都没关系(数据库看起来将是相同的,并且如果数据访问不相同,则数据访问将是相似的).根据我的经验,最好不要弄乱Django用户模型:

You only need to add a m2m field on SeriesText. Fundamentally, it doesn't matter which model a many-to-many field is attached to (the database will look the same and the data access will be similar if not identical). Based on my experience, it's better if you don't have to mess with the Django User model:

class SeriesText(models.Model):
    series_name = models.CharField(max_length=255)
    series_text = models.CharField(max_length=255)
    subscribers = models.ManyToManyField(User, related_name='subscriptions')

    def __str__(self):
        return self.series_name

要查看其工作原理,请首先添加一些数据:

To see how that works, first add some data:

s1 = SeriesText.objects.create(series_name='name1', series_text='text1')
s2 = SeriesText.objects.create(series_name='name2', series_text='text2')

u1 = User.objects.create_user('user1')
u2 = User.objects.create_user(username='user2')
u3 = User.objects.create_user(username='user3')
u4 = User.objects.create_user(username='user4')
u5 = User.objects.create_user(username='user5')

s1.subscribers.add(u1, u2, u3)
s2.subscribers.add(u3, u4, u5)

然后获取SeriesText的所有订阅者(我在这里从数据库中获取对象,以表明这不是上面最后两行的伪像-数据库中的数据已更改):

Then to fetch all subscribers for a SeriesText (I'm fetching the objects from the database here to show that it is not an artefact of the last two lines above - data has been changed in the database):

>>> SeriesText.objects.get(series_name='name1').subscribers.all()
[<User: user1>, <User: user2>, <User: user3>]

>>> SeriesText.objects.get(series_name='name2').subscribers.all()
[<User: user3>, <User: user4>, <User: user5>]

获取用户的所有订阅(我们声明在多对多字段中,从User到SeriesText的关系应命名为 subscriptions ):

to fetch all subscriptions for a user (we declared that the relation from User to SeriesText should be named subscriptions in the many-to-many field):

>>> User.objects.get(username='user1').subscriptions.all()
[<SeriesText: name1>]
>>> User.objects.get(username='user2').subscriptions.all()
[<SeriesText: name1>]
>>> User.objects.get(username='user3').subscriptions.all()
[<SeriesText: name1>, <SeriesText: name2>]
>>> User.objects.get(username='user4').subscriptions.all()
[<SeriesText: name2>]
>>> User.objects.get(username='user5').subscriptions.all()
[<SeriesText: name2>]

如果我们没有声明related_name,我们将不得不使用默认值:

If we hadn't declared the related_name we would have had to use the default:

User.objects.get(username='user1').seriestext_set.all()

阅读效果不佳.

这篇关于Django与用户默认身份验证模型和另一个模型的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆