在 Django Rest Framework 逻辑中创建相关模型的主要问题是很难跟踪哪些模型实例对应于哪些视图。 如果视图需要访问来自多个模型的数据,或者如果模型需要在视图更改时自动更新,这可能会成为一个问题。
我有两个模型:
class User(models.Model):
name = models.CharField(max_length=255)
email = models.EmailField()
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
def __str__(self):
return self.title
class Meta: #noqa (ignore flake8 E501) This is needed for related name to work in Django 2+ https://stackoverflow.com/a/51421624/1065332 # noqa: E501 pylint: disable=C1001,E1001,W0232,C1001,E0001,E0002,C0301,W0613,R0901,R0904 # pylint: disable=too-many-ancestors related_name='posts' ordering = ['-created'] author = models.ForeignKey('User', on_delete=models.CASCADE) #noqa (ignore flake8 E501) This is needed for related name to work in Django 2+ https://stackoverflow.com/a/51421624/1065332 # noqa: E501 pylint: disable=C1001,E1001,W0232,C1001,E0001 created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) slug = AutoSlugField('slug', max_length=50 , unique=True , populate_from='title') tags = TaggableManager() likes = GenericRelation('LikeDislike') views = GenericRelation('ViewCount') comments = GenericRelation('Comment') image1 image2 image3 image4
我想创建一个视图集来显示所有帖子以及创建它的用户数据,但我不知道该怎么做。 我试过了,但没用。 有什么建议么 ? 提前感谢! 😀😀😀😀😀😀 <3 <3 <3 <3 <3 <3 <3 <3 🙂 🙂 🙂 🙂 🙂 🙂 🙂 ;)))))))。 到目前为止,这是我的视图集代码。 我正在使用 django rest 框架逻辑。 再次感谢 ! 我感谢您的帮助 ! 祝你今天愉快 ! ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^^ ))))))))))))))))))。 另外请记住,我是新手,我正在学习,所以如果您发现任何错误或错误或类似的事情,请耐心等待我,因为我每天都在学习并努力提高自己! 再一次非常感谢你! 有一个很好的一天! 🙂🙂🙂🙂🙂🙂🙂🙂😉😉😉😉😉😉)))))。 另外请记住,我是新手,我正在学习,所以如果您发现任何错误或错误或类似的事情,请耐心等待我,因为我每天都在学习并努力提高自己! 再一次非常感谢你! 祝你今天过得愉快!:-)-:-)-:-)-:-)-:-)-;-;-;-;-;-;-((((((((((((( (((((((((()。 另外请记住,我是新手,我正在学习,所以如果您发现任何错误或错误或类似的事情,请耐心等待我,因为我每天都在学习并努力提高自己! 再一次非常感谢你! 祝你今天过得愉快!:))))))。 到目前为止,这是我的视图集代码。 我正在使用 django rest 框架逻辑。 再次感谢 ! 我感谢您的帮助 ! 祝你今天愉快 ! ^^^^^^^^^^^^^^^^^^^^^^^^)。 还请记住,我是新来的,我是一个新的学习者,如果你喜欢我,我会和我一起学习,因为我仍然在学习和尝试,我每天都在学习,谢谢你,我很高兴,我很高兴!:) )))))。 到目前为止,这是我的视图集代码。 我正在使用 django rest 框架逻辑。谢谢 agian!感谢您的帮助!Haveaniceday!:))))).Hereismyviewsetcodesofar.:Iamusingdjangorestframeworklogic。 .:Thanksagain!:IAppreciateyourhelp!:Haveaniceday!.Alsopleasekeepinmindthatiamnewatthisandiamlearningasigoopleasebepatientwithmeifyouseemistakesorerrorsoranthinglikethatbecauseiamstilllearningandtryingtoimprovemyselfeverydaythankyouverymuchagainhaveaniceday!.Alsopleasekeepinmindthatiamnewatthisandiamlearningasigoopleasebepatientwithmeifyouseemistakesorerrorsoranthinglikethatbecauseiamstilllearningandtryingtoimprovemyselfeverydaythankyouverymuchagainhaveaniceday!.Alsopleasekeepinmindthatiamnewatthisandiamlearningasigoopleasebepatientwithmeifyouseemistakesorerrorsoranthinglikethatbecauseiamstilllearningandtryingtoimprovemyselfeverydaythankyouverymuchagainhaveaniceday!.Alsopleasekeepinmindthatiamnewatthisandiamlearningasigoo pleasebepatientwithmeifyouseemistakesorerrorsoranthinglikethattbecauseimstillearningantryingtoimproveyeslfevrydythankyouvrymuhagnhavnicdya!.Alspleepepiaetwitiadthattimneawatshiadnamlearnigasgoolepesebpaitenwitiemyfioyuseeemisaktesoerrrosoraenthignlkiehattbecausiemstillearningantryingtopiromveyeslfevrydythankyouvrymuhagnhavnicdya!.Alspleepepiaetwitiadthattimneawatshiadnamlearnigasgoolepesebpaitenwitiemyfioyuseeemisaktesoerrrosoraenthignlkiehattbecausiemstillearningantryingtopiromveyeslfevrydythankyouvrymuhagnhavnicdya!.Alspleepepiaetwitiadthattimneawatshiadnamlearnigasgoolepesebpaitenwitiemyfioyuseeemisaktesoerrrosoraenthignlkiehattbecausiemstillearningantryingtopiromveyeslfevrydythankyouvrymuhagnhavnicdya!.Alspleepepiaetwitiadthattimneawatshiadnamlearnigasgoolepesebpaitenwitiemyfioyuseeemisaktesoerrrosoraenthignlkiehattbecausiemstillearningantryingtopiromveyeslfevrydythankyouvrymuhagnhavnicdya!.Alspleepepiaetwitiadthattimneawatshiadnamlearnigasgoolepesebpaitenwitiemyfioyuseeemisaktesoe rrrosoraenthignlkiehattbecausiemsstilllearningantryingtopiromveyeslfevrydy 谢谢你 v ry mu hag n ha v nic d ya!! Al s ple e pi a et wi ti ad that tim ne aw at shi ad na m le ar nig as go ol e pe se bpai XNUMX wi ti em yf io yu se e mi sakte so err ro so ran thing like e 这样的事Hat t becau si em st il le ar ning g ant ryi ng it om ve yes lf ev ryd y thank yo uv ry mu hag n ha v nic d ya!! Al s ple e pi a et wi ti ad that tim ne aw at shi ad na m le ar nig as go ol e pe se bpai XNUMX wi ti em yf io yu se e mi sakte so err ro so ran thing like e 这样的事帽子 t 因为 si em st il le ar ning g ant ryi ng ir om ve yes lf ev ryd y thank yo uv ry mu hag n ha v nic d ya!![/code] Al s ple e pi a et wi ti ad that tim neaw at shi ad nam le ar nig as go ol e pe se bpai XNUMX wi ti em yf io yu se e mi sakte so err ro so ran thing like e hat t becau si em still le ar ning ant ryi ng top ir om ve yes lf ev ryd y thank you uv ry mu hag n ha v nic d ya!! Al s ple e pi a et wi ti ad that tim ne aw at shi ad na m le ar nig as go ol e pe se bpai XNUMX wi ti em yf io yu se e mi sakte so err ro so ran thing like e 这样的事Hat t becau si em st il le ar ning g ant ryi ng it om ve yes lf ev ryd y thank yo uv ry mu hag n ha v nic d ya!! Al s ple e pi a et wi ti ad that tim ne aw at shi ad na m le ar nig as go ol e pe se bpai XNUMX wi ti em yf io yu se e mi sakte so err ro so ran thing like e 这样的事Hat t becau si em st il le ar ning g ant ryi ng it om ve yes lf ev ryd y thank yo uv ry mu hag n ha v nic d ya!! Alspleepepiaetwitadthattimneawatshiadnamlearnigasgoolepesebpaitenwitemyfioyuseeemisaktesoerrrosoranthinglikethatbecauseimstillearningantryingtoimproveyeslfevrydythankyouvrymuhagnhavnicdya!.Alspleepepiaetwitadthattimneawatshiadnamlearnigasgoolepesebpaitenwitemyfioyuseeemisaktesoerrrosoranthinglikethatbecauseimstillearningantryingtoimproveyeslfevrydythankyouvrymuhagnhavnicdya!.Alspleepepiaetwitadthattimneawatshiadnamlearnigasgoolepesebpaitenwitemyfioyuseeemisaktesoerrrosoranthinglikethatbecauseimstillearningantryingtoimproveyeslfevrydythankyouvrymuhagnhavnicdya!.Alspleepepiaetwitadthattimneawatshiadnamlearnigasgoolepesebpaitenwitemyfioyuseeemisaktesoerrrosoranthinglikethatbecauseimstillearningantryingtoimproveyeslfevrydythankyouvrymuhagnhavnicdyathankyouverymuchagainhaveaniceday!:)))))).Hereismyviewsetcodesofar.:Iamusingdjangorestframeworklogic.:Thanksagain!:IAppreciateyourhelp!:Haveaniceday!:)))))).Hereismyviewsetcodesofar.:Iamusingdjangorestframeworklogic.:Thanksagain!:我感谢你elp!:祝你有个愉快的一天!
这段代码定义了两个模型,User 和 Post。 User 模型有姓名和电子邮件字段,而 Post 模型有标题、正文、作者、创建、更新、slug、标签、喜欢、视图和评论字段。 该代码还为 Post 模型定义了一个 Meta 类,并带有 related_name 属性。
Django REST Framework 中的模型关系
在 Django REST Framework 中,模型关系由模型中相互关联的一组字段表示。 例如,表示用户的模型可能包含姓名和电子邮件地址字段。 如果用户更改了他们的电子邮件地址,框架也会自动更新名称字段。
要创建模型关系,您首先需要在您的一个模型中创建一个字段来表示两个模型之间的关系。 该字段的名称将与其他模型中的字段名称相同。 例如,如果我们想在我们的用户和电子邮件地址模型之间创建一个模型关系,我们将在我们的用户模型中创建一个名为 id_user 的字段,并在我们的电子邮件地址模型中将其称为 id_email。
接下来,您需要在其他模型中创建一个关联字段来表示将存储在此关系中的数据。 此关联字段应与第一个模型中的字段同名,但要在其后附加下划线 (_)。 例如,如果我们想为每个用户存储一个 ID 号,我们将在我们的用户模型中创建一个 id_user 字段,并在我们的电子邮件地址模型中将其命名为 id_id。
最后,您需要向 firstmodel 添加一个 relation() 方法,该方法返回一个元组列表,表示 secondmodel 中字段之间的所有关系。 例如,如果我们想找到所有电子邮件地址以“test”结尾的用户,我们可以像这样使用 relation() :
def relation(self, other): return [ (id_user, id_email) for (id_user, id_email) in self._get_all() ]
什么是 REST 框架
REST Framework 是一个用于在 Django 中创建 RESTful Web 服务的库。 它提供了一种简单且一致的方式来创建可供其他应用程序使用的 Web 服务。