sid8491 sid8491 - 2 months ago 6
MySQL Question

What is Django ORM equivalent of SELECT col1 FROM table WHERE col2=somestring?


class ShowDetail(models.Model):
title = models.CharField(max_length=100)

class SeasonDetail(models.Model):
title = models.ForeignKey(ShowDetail, on_delete=models.CASCADE)

class EpisodeDetail(models.Model):
showTitle = models.ForeignKey(ShowDetail, on_delete=models.CASCADE)
seasonNumber = models.ForeignKey(SeasonDetail, on_delete=models.CASCADE)
episodeTitle = models.CharField(max_length=100, null=True, blank=True)
episodeNumber = models.CharField(max_length=5, null=True, blank=True)


episode_update.seasonNumber = SeasonDetail.objects.filter(title=showname).values('season')

I want to check the season value (01, 02, 03 etc) are present for given tv-show (Arrow, The Flash etc).

I am getting below error, when I try to update the season value in EpisodeDetail model:

invalid literal for int() with base 10: 'The Flash'

When I am using

episode_update.seasonNumber = SeasonDetail.objects.filter(title=showname).values_list('season', flat=True)

Getting error

invalid literal for int() with base 10: 'The Flash'

And when I use

episode_update.seasonNumber = SeasonDetail.objects.filter(title=ShowDetail.objects.get(title=showname)).values_list('season', flat=True)

I am getting

Cannot assign "['01']": "EpisodeDetail.seasonNumber" must be a "SeasonDetail" instance.


Yep, you can use values() queryset method.



If you want a raw representation, you need to use values_list along flat=True


This returns

[01, 02, 03,....]

UPDATE 2 In your model seasonNumber is a foreign key to a SeasonDetail instance, you are trying to assign a list. So, remove the value_list() and get the first one.

episode_update.seasonNumber = SeasonDetail.objects.filter(title=showname)[0]