如何与其他用户共享我的列表(Django)

发布于 2025-01-15 18:29:39 字数 1885 浏览 1 评论 0原文

大家好,我是 Django 的初学者,我通过 YouTube 上的视频制作了一个购物清单应用程序。我可以在我的应用程序中创建列表,并且想将我的列表分享给其他用户,但我不知道如何真正做到这一点。如果有人能帮助我,我会很高兴。

模型.py 我尝试使用 models.ManyToManyField 但我不知道我应该在 View.py 中做什么来将列表从用户应用到其他用户

from django.db import models
from django.db import models
from django.contrib.auth.models import User


class Liste(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    title = models.CharField(max_length=30, verbose_name = "Titel")
    item1 = models.CharField(max_length=30, blank=True, verbose_name = "1")
    preis1 = models.DecimalField(max_digits=5, decimal_places=2, default = 0, verbose_name = "Preis")
    item2 = models.CharField(max_length=30, blank=True, verbose_name = "2")
    preis2 = models.DecimalField(max_digits=5, decimal_places=2, default = 0, verbose_name = "Preis")
    item3 = models.CharField(max_length=30, blank=True, verbose_name = "3")
    preis3 = models.DecimalField(max_digits=5, decimal_places=2, default = 0, verbose_name = "Preis")
    item4 = models.CharField(max_length=30, blank=True, verbose_name = "4")
    preis4 = models.DecimalField(max_digits=5, decimal_places=2, default = 0, verbose_name = "Preis")
    item5 = models.CharField(max_length=30, blank=True, verbose_name = "5")
    preis5 = models.DecimalField(max_digits=5, decimal_places=2, default = 0, verbose_name = "Preis")

    @property
    def rate(self):
        total = (self.preis1) + (self.preis2) + (self.preis3) + (self.preis4) + (self.preis5)   
        return total

    def __str__(self):    
        return self.title    


class Share(models.Model):
    user = models.ManyToManyField(User)

这是我的列表

这里我想将列表发送给其他用户

Hello guys i'm a begginer in Django and i made a Shopping-list-app from wathing videos on youtube. I can make list in my app and i want to share my list to other user, but i do't know how really do that. I would be happy if someone could help me.

model.py
I tried to use models.ManyToManyField but i know don't what should i do in View.py to app the list from a user to other user

from django.db import models
from django.db import models
from django.contrib.auth.models import User


class Liste(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    title = models.CharField(max_length=30, verbose_name = "Titel")
    item1 = models.CharField(max_length=30, blank=True, verbose_name = "1")
    preis1 = models.DecimalField(max_digits=5, decimal_places=2, default = 0, verbose_name = "Preis")
    item2 = models.CharField(max_length=30, blank=True, verbose_name = "2")
    preis2 = models.DecimalField(max_digits=5, decimal_places=2, default = 0, verbose_name = "Preis")
    item3 = models.CharField(max_length=30, blank=True, verbose_name = "3")
    preis3 = models.DecimalField(max_digits=5, decimal_places=2, default = 0, verbose_name = "Preis")
    item4 = models.CharField(max_length=30, blank=True, verbose_name = "4")
    preis4 = models.DecimalField(max_digits=5, decimal_places=2, default = 0, verbose_name = "Preis")
    item5 = models.CharField(max_length=30, blank=True, verbose_name = "5")
    preis5 = models.DecimalField(max_digits=5, decimal_places=2, default = 0, verbose_name = "Preis")

    @property
    def rate(self):
        total = (self.preis1) + (self.preis2) + (self.preis3) + (self.preis4) + (self.preis5)   
        return total

    def __str__(self):    
        return self.title    


class Share(models.Model):
    user = models.ManyToManyField(User)

This is my List

And here i want to send the list to other user

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东走西顾 2025-01-22 18:29:39

逻辑需要一些升级,以便您可以到达您想要的点,

from django.db import models
from django.db import models
from django.contrib.auth.models import User

class Liste(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    title = models.CharField(max_length=30, verbose_name = "Titel")

class Item(models.Model):
    liste = models.ForeignKey(Liste,on_delete=models.CASCADE)
    item = models.CharField(max_length=30, verbose_name="Title")
    preis = models.DecimalField(max_digits=5, decimal_places=2,default=0,verbose_name="Preis")

然后您可以将多个项目添加到列表中,您可以使用 JavaScript 在模板上计算每个列表的总数,并且它将让用户按照您的描述进行选择

the logic need some upgrading so you can get to the point you want ,

from django.db import models
from django.db import models
from django.contrib.auth.models import User

class Liste(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    title = models.CharField(max_length=30, verbose_name = "Titel")

class Item(models.Model):
    liste = models.ForeignKey(Liste,on_delete=models.CASCADE)
    item = models.CharField(max_length=30, verbose_name="Title")
    preis = models.DecimalField(max_digits=5, decimal_places=2,default=0,verbose_name="Preis")

Then you can add multiple item to the list and you can use JavaScript to calculate on template the total of each list , and it will have the user selected as you describe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文