猿问

Django 多对多字段 add() 得到了一个意想不到的关键字参数 “id”

我想将项目从可用经典添加到所选经典

我怎么能这样做,如下图所示

我可以得到选择经典


Profile.objects.get(user=request.user).classic.add(id=2)


但我无法将iems从可用经典添加到所选经典


任何人都可以帮助这个问题快速请感谢所有


Models.py


from django.db import models


# Create your models here.

from django.utils import timezone

from django.contrib.auth.models import User

from django.db.models.signals import post_save

from django.dispatch import receiver



class Language(models.Model):

    language = models.CharField(

        max_length=2, 

        choices=[

            ('AR', 'Arabic'),

            ('EN', 'English'),

        ],

        default='AR'

    )


    def __str__(self):

        return self.language



class 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.name



class 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.py


from django.contrib import admin


# Register your models here.

from . import models


噜噜哒
浏览 78回答 1
1回答

当年话下

桑特与我合作Profile.objects.get(user=request.user).classic.add(Classic.objects.get(id=1))
随时随地看视频慕课网APP

相关分类

Python
我要回答