简体   繁体   中英

Django cannot assign variable to objects

when i'm trying to add data to uer in many to many as showen in views code below

views.py

def playList(request, name, language):
    playlist = {
        'classic': Classic,
    }
    playlist2 = {
        'classic': 'classic',
    }
    num = {
        'ar': 1,
        'en': 2,
    }
    if request.method == 'POST':
        form2 = FavoriteForm(request.POST)
        if form2.is_valid():
            x = Profile.objects.get(user=request.user)
            x.playlist2[name].add(playlist[name].objects.get(id=request.POST.get(playlist2[name])))
    form = playlist[name].objects.filter(lang=num[language])
    context = {}
    for i in form:
        context[i] = i
    context = {
        'form': form,
        'playlist': playlist2[name],

    }
    return render(request, 'playlist.html', context)

get this error that str object has no attr objects

'str' object has no attribute 'objects'

when i'm change str to istance

as showen in views code below

views.py

def playList(request, name, language):
    playlist = {
        'classic': Classic,
    }
    playlist2 = {
        'classic': Profile.classic,
    }
    num = {
        'ar': 1,
        'en': 2,
    }
    if request.method == 'POST':
        form2 = FavoriteForm(request.POST)
        if form2.is_valid():
            x = Profile.objects.get(user=request.user)
            x.playlist2[name].add(playlist[name].objects.get(id=request.POST.get(playlist2[name])))
    form = playlist[name].objects.filter(lang=num[language])
    context = {}
    for i in form:
        context[i] = i
    context = {
        'form': form,
        'playlist': playlist2[name],

    }
    return render(request, 'playlist.html', context)

i get this error that profile has no attr playlist2

'Profile' object has no attribute 'playlist2'

and this is my model code

modles.py

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)
    workOut = models.ManyToManyField(WorkOut, blank=True)
    chillOut = models.ManyToManyField(ChillOut, blank=True)
    romantic = models.ManyToManyField(Romantic, blank=True)
    happy = models.ManyToManyField(Happy, blank=True)
    sad = models.ManyToManyField(Sad, blank=True)

    def __str__(self):
        return str(self.user)

You can not use x.playlist2[name].add , since that will look for the playlist2 attribute of x .

You can make use of getattr(..) [python-doc] . If you perform getattr(x, 'y') , then this is equivalent to xy :

def playList(request, name, language):
    playlist = {
        'classic': Classic,
    }
    playlist2 = {
        'classic': ,
    }
    num = {
        'ar': 1,
        'en': 2,
    }
    if request.method == 'POST':
        form2 = FavoriteForm(request.POST)
        if form2.is_valid():
            x = Profile.objects.get(user=request.user)
            .add(playlist[name].objects.get(id=request.POST.get(playlist2[name])))
    form = playlist[name].objects.filter(lang=num[language])
    context = {}
    for i in form:
        context[i] = i
    context = {
        'form': form,
        'playlist': playlist2[name],

    }
    return render(request, 'playlist.html', context)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM