have the following models
Class FootballWebsite(models.Model):
"""Football service website."""
url = models.URLField(validators=[validate_club_url], unique=True)
#Football service
id = models.CharField(primary_key=True, unique=True)
sub_categories = models.ForeignKey(SubCategory, default=1)
class SubCategory(models.Model):
"""Sub Category"""
category = models.ForeignKey(Category)
name = models.CharField(max_length=50)
slug = models.SlugField()
class FootballWebsiteDescription(models.Model):
"""Football service website description."""
about = models.ForeignKey(Footballwebsite)
title = models.TextField(blank=True, null=True)
keywords = models.TextField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
relation = models.URLField(blank=True, null=True)
subject = models.TextField(blank=True, null=True)
type = models.TextField(blank=True, null=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=True)
language = models.TextField(null=True, blank=True)
contactInformation = models.TextField(null=True, blank=True)
officialInfo = models.BooleanField(default=False)
and trying to execute the following
class Command(BaseCommand):
def handle(self, *args, **options):
websites = FootballWebsiteDescription.objects.filter(title__in=['title1',' title2'])
for website in websites:
try:
fc = FootballWebsite.objects.filter(id=website.about_id)
fc.sub_categories_id = 88
fc.save()
and also the following option
fc = FootballWebsite.objects.filter(id=website.about_id).update(sub_categories_id=88)
to my understanding both ways to update the subcategory id from the default 1 to 88 should work. i can update other fields this way at least, and see this as an example referenced anywhere.
i can only find this reference Django: Set foreign key using integer? which indicates i might have run into a bug in my very old Django 1.7 project.
Am i missing something very obvious here or is there a work around? much obliged.
EDIT!! ended up with the following that works for me.
class Command(BaseCommand):
def handle(self, *args, **options):
websites = FootballWebsiteDescription.objects.filter(title__in=['title1',' title2'])
for website in websites:
try:
sub_category_instance = SubCategory.objects.get(id=88)
fc = FootballWebsite.objects.filter(id=website.about_id,sub_categories_id=1)[0]
fc.sub_categories = sub_category_instance
fc.sub_categories.save()
fc.save()
except Exception:
pass