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" ...
How to add extended user model fields to user creation form for registration?
I currently have a registration page which uses the UserCreationForm to obtain: username, email, first name, last name, password1 and password2. I wanted to extend this to include a date of birth field however I am not sure how to ensure that the date of birth field is included in the registration form.
My forms.py which defines what fields I want the user to register with:
class UserRegisterForm(UserCreationForm):
email = forms.EmailField(required=True)
first_name = forms.CharField(required=True, max_length=50)
last_name = forms.CharField(required=True, max_length=50)
#Meta inspects the current model of the class User, then ensures that 6 of the fields inside of it are there
class Meta:
model = User
fields = ['username', 'email', 'first_name', 'last_name', 'password1', 'password2']
My views.py which uses the UserRegisterForm to register the user:
/r/djangolearning
https://redd.it/a8ah9b
I currently have a registration page which uses the UserCreationForm to obtain: username, email, first name, last name, password1 and password2. I wanted to extend this to include a date of birth field however I am not sure how to ensure that the date of birth field is included in the registration form.
My forms.py which defines what fields I want the user to register with:
class UserRegisterForm(UserCreationForm):
email = forms.EmailField(required=True)
first_name = forms.CharField(required=True, max_length=50)
last_name = forms.CharField(required=True, max_length=50)
#Meta inspects the current model of the class User, then ensures that 6 of the fields inside of it are there
class Meta:
model = User
fields = ['username', 'email', 'first_name', 'last_name', 'password1', 'password2']
My views.py which uses the UserRegisterForm to register the user:
/r/djangolearning
https://redd.it/a8ah9b
reddit
r/djangolearning - How to add extended user model fields to user creation form for registration?
3 votes and 7 comments so far on Reddit