Search code examples
pythondjangodjango-modelsdjango-viewsmany-to-many

Django Many to Many Field add() got an unexpected keyword argument 'id'


I want to add items from Available classic to Chosen classic

how can i do that as in image below

Djano admin panel

i can get Chosen classic by

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

but i can't add iems from Available classic to Chosen classic

any one can help this problem fast please Thanks for all

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


class ClassicAdmin(admin.TabularInline):
    model = models.Classic

class PlayLists(admin.ModelAdmin):
    inlines = [ClassicAdmin]

class Favo(admin.ModelAdmin):
    filter_horizontal = ['classic']


admin.site.register(models.Language, PlayLists)
admin.site.register(models.Profile, Favo)

what's wrong with in my code thank for all


Solution

  • Thant's works with me

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