Adding a ForeignKey to an existing model: TypeError: int()???
Hi All,
I have a model 'Job' with existing data (10,000+) that has a job_category field. It is currently a choice field from a hardcoded tuple"
job_category = models.CharField(max_length=255, choices=job_category_choices, blank=True)
I want to make a new model 'JobCategory', replace the 'hardcoded tuple' job_category field with a ForeignKey
job_category_2 = models.ForeignKey(JobCategory, on_delete = models.CASCADE)
I populated the JobCategory model with this in the shell:
for cat in job_category_choices:
new_job_category = JobCategory.objects.create(name=cat[0])
print(new_job_category.name + ' | ' + new_job_category.slug)
**models.py is at end of post. Hopefully formatting works.**
When I make migrations it's OK, when I migrate I get this error:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'CharField'
I can't figure out what I'm doing wrong
models.py snippet:
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Count
from django.template.defaultfilters import slugify
from django.utils import timezone
from django.utils.html import strip_tags
job_category_choices = (
('ACCOUNTING', 'Accounting'),
('ARCHITECT', 'Architect'),
#...300 more not shown,
)
class Timestamp(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True #don't add a table
### JobCategory populated with ###
#for cat in job_category_choices:
# new_job_category = JobCategory.objects.create(name=cat[0])
# print(new_job_category.name + ' | ' + new_job_category.slug)
class JobCategory(models.Model):
name = models.CharField(max_length=255, choices=job_category_choices, blank=True)
#meta
slug = models.SlugField()
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.name)
super(JobCategory, self).save()
def __str__(self):
return self.name
class Job(Timestamp):
# MOVING job_category_choices TO OUTSIDE THE JOB CLASS, ALTHOUGH IT'S CURRENTLY STILL IN THE CLASS
job_category_choices = (
('ACCOUNTING', 'Accounting'), ('ARCHITECT', 'Architect'), #300 more not shown
)
name = models.CharField("Job Title", max_length=255, blank=False)
company = models.CharField("Company Name", max_length=255, blank=False)
description = models.TextField("Job Description", blank=False)
job_country = models.CharField(max_length=255, blank=True)
job_location = models.CharField(max_length=255, blank=True)
# CURRENT FIELD
job_category = models.CharField(max_length=255, choices=job_category_choices, blank=True)
# NEW FIELD:
job_category_2 = models.ForeignKey(JobCategory, on_delete = models.CASCADE)
#Meta
slug = models.SlugField(unique=True, max_length=255)
approved = models.BooleanField(default=False)
upgraded = models.BooleanField(default=False)
stripe_id = models.CharField(max_length=255, blank=True)
/r/djangolearning
https://redd.it/75e2z1
Hi All,
I have a model 'Job' with existing data (10,000+) that has a job_category field. It is currently a choice field from a hardcoded tuple"
job_category = models.CharField(max_length=255, choices=job_category_choices, blank=True)
I want to make a new model 'JobCategory', replace the 'hardcoded tuple' job_category field with a ForeignKey
job_category_2 = models.ForeignKey(JobCategory, on_delete = models.CASCADE)
I populated the JobCategory model with this in the shell:
for cat in job_category_choices:
new_job_category = JobCategory.objects.create(name=cat[0])
print(new_job_category.name + ' | ' + new_job_category.slug)
**models.py is at end of post. Hopefully formatting works.**
When I make migrations it's OK, when I migrate I get this error:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'CharField'
I can't figure out what I'm doing wrong
models.py snippet:
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Count
from django.template.defaultfilters import slugify
from django.utils import timezone
from django.utils.html import strip_tags
job_category_choices = (
('ACCOUNTING', 'Accounting'),
('ARCHITECT', 'Architect'),
#...300 more not shown,
)
class Timestamp(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True #don't add a table
### JobCategory populated with ###
#for cat in job_category_choices:
# new_job_category = JobCategory.objects.create(name=cat[0])
# print(new_job_category.name + ' | ' + new_job_category.slug)
class JobCategory(models.Model):
name = models.CharField(max_length=255, choices=job_category_choices, blank=True)
#meta
slug = models.SlugField()
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.name)
super(JobCategory, self).save()
def __str__(self):
return self.name
class Job(Timestamp):
# MOVING job_category_choices TO OUTSIDE THE JOB CLASS, ALTHOUGH IT'S CURRENTLY STILL IN THE CLASS
job_category_choices = (
('ACCOUNTING', 'Accounting'), ('ARCHITECT', 'Architect'), #300 more not shown
)
name = models.CharField("Job Title", max_length=255, blank=False)
company = models.CharField("Company Name", max_length=255, blank=False)
description = models.TextField("Job Description", blank=False)
job_country = models.CharField(max_length=255, blank=True)
job_location = models.CharField(max_length=255, blank=True)
# CURRENT FIELD
job_category = models.CharField(max_length=255, choices=job_category_choices, blank=True)
# NEW FIELD:
job_category_2 = models.ForeignKey(JobCategory, on_delete = models.CASCADE)
#Meta
slug = models.SlugField(unique=True, max_length=255)
approved = models.BooleanField(default=False)
upgraded = models.BooleanField(default=False)
stripe_id = models.CharField(max_length=255, blank=True)
/r/djangolearning
https://redd.it/75e2z1
reddit
Adding a ForeignKey to an existing model:... • r/djangolearning
Hi All, I have a model 'Job' with existing data (10,000+) that has a job_category field. It is currently a choice field from a hardcoded tuple" ...
I just made the most 50/50 script ever: it selects and opens random image URLs from 4chan (returns NSFW results like half of the time)
It's fun to run! Cause you *really* never know what's going to pop up. Run at your own risk though, cause it can return anything from cute kitten pictures, to not-unseeable NSFL pictures
#!/usr/bin/python3
#*************************************************************************************************************************
#IMPORTANT
#Don't remove the time.sleeps; which are in place to comply with 4chan's API rule of 'no more than 1 request per second'
#https://github.com/4chan/4chan-API
#
#This script selects a random images from 4chan, and opens them in web browser
#Requires the 'requests' module
#*************************************************************************************************************************
import requests,random,json,time,webbrowser
#Returns [ random image URL, random image's thread URL ]
def r4chan():
#List of 4chan boards
boards = ['a','c','w','m','cgl','cm','n','jp','vp','v','vg','vr','co','g','tv','k','o','an','tg','sp','asp','sci','int','out','toy','biz','i','po','p','ck','ic','wg','mu','fa','3','gd','diy','wsg','s','hc','hm','h','e','u','d','y','t','hr','gif','trv','fit','x','lit','adv','lgbt','mlp','b','r','r9k','pol','soc','s4s']
#Select a board
board = random.choice(boards)
#Request board catalog, and get get a list of threads on the board; then sleeping for 1.5 seconds
threadnums = list()
/r/Python
https://redd.it/ccrh6o
It's fun to run! Cause you *really* never know what's going to pop up. Run at your own risk though, cause it can return anything from cute kitten pictures, to not-unseeable NSFL pictures
#!/usr/bin/python3
#*************************************************************************************************************************
#IMPORTANT
#Don't remove the time.sleeps; which are in place to comply with 4chan's API rule of 'no more than 1 request per second'
#https://github.com/4chan/4chan-API
#
#This script selects a random images from 4chan, and opens them in web browser
#Requires the 'requests' module
#*************************************************************************************************************************
import requests,random,json,time,webbrowser
#Returns [ random image URL, random image's thread URL ]
def r4chan():
#List of 4chan boards
boards = ['a','c','w','m','cgl','cm','n','jp','vp','v','vg','vr','co','g','tv','k','o','an','tg','sp','asp','sci','int','out','toy','biz','i','po','p','ck','ic','wg','mu','fa','3','gd','diy','wsg','s','hc','hm','h','e','u','d','y','t','hr','gif','trv','fit','x','lit','adv','lgbt','mlp','b','r','r9k','pol','soc','s4s']
#Select a board
board = random.choice(boards)
#Request board catalog, and get get a list of threads on the board; then sleeping for 1.5 seconds
threadnums = list()
/r/Python
https://redd.it/ccrh6o
reddit
r/Python - I just made the most 50/50 script ever: it selects and opens random image URLs from 4chan (returns NSFW results like…
134 votes and 24 comments so far on Reddit