我想将项目从可用经典添加到所选经典我怎么能这样做,如下图所示我可以得到选择经典Profile.objects.get(user=request.user).classic.add(id=2)但我无法将iems从可用经典添加到所选经典任何人都可以帮助这个问题快速请感谢所有Models.pyfrom django.db import models# Create your models here.from django.utils import timezonefrom django.contrib.auth.models import Userfrom django.db.models.signals import post_savefrom django.dispatch import receiverclass Language(models.Model): language = models.CharField( max_length=2, choices=[ ('AR', 'Arabic'), ('EN', 'English'), ], default='AR' ) def __str__(self): return self.languageclass Classic(models.Model): name = models.CharField(max_length=50, blank=False, null=False) music = models.FileField(upload_to='', max_length=100, blank=True, null=True) lang = models.ForeignKey(Language, on_delete=models.CASCADE) def __str__(self): return self.nameclass Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) classic = models.ManyToManyField(Classic, blank=True, null=True) workOut = models.ManyToManyField(WorkOut, blank=True, null=True) chillOut = models.ManyToManyField(ChillOut, blank=True, null=True) romantic = models.ManyToManyField(Romantic, blank=True, null=True) happy = models.ManyToManyField(Happy, blank=True, null=True) sad = models.ManyToManyField(Sad, blank=True, null=True) lang = models.ManyToManyField(Language, blank=True, null=True) def __str__(self): return str(self.user)def update_user_profile(sender, **kwargs): if kwargs['created']: user = Profile.objects.create(user=kwargs['instance'])post_save.connect(update_user_profile,sender=User)Admin.pyfrom django.contrib import admin# Register your models here.from . import models
1 回答
当年话下
TA贡献1890条经验 获得超9个赞
桑特与我合作
Profile.objects.get(user=request.user).classic.add(Classic.objects.get(id=1))
添加回答
举报
0/150
提交
取消