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" ...
Best strategies for incorporating data from a RESTful api: forms vs serializers?
**tl;dr**
When pulling data into my django database from an api, should I use forms, or a [django rest framework](http://www.django-rest-framework.org/) serializer?
**What I did**
Based on some discussion in [another thread](https://www.reddit.com/r/django/comments/7e0olm/how_to_save_json_api_response_in_database/), I built an ultra-simple example of how to extract data from an api, in this case the api for the video game Destiny 2:
https://github.com/cortical-iv/restit
Note this is essentially a port of UltimateDjango's [How to Consume REST APIs with Django & Python Requests](https://ultimatedjango.com/blog/how-to-consume-rest-apis-with-django-python-reques/).
My goal was to follow Section 12.1 of Two Scoops, and *Validate all incoming data with django forms.* But what I actually did was a two step process that I'm now thinking was sort of dumb (I pasted the code below the fold):
1. Bind data about path parameters in the url into a form.
2. After extracting the needed data from the response to `requests`, use a django rest framework *serializer* to validate and save that data.
Now that this is up and working, I realize I didn't actually really follow the advice from Two Scoops of importing through forms. I imported required *path* information (user name) through a form, but then did all the heavy lifting with the serializer. I now think it would be better to replace step 2 with another form (a SaveUser form) that itself includes all the fields I need in my database.
**Why I don't like what I did**
Why am I thinking my approach smells? A couple of reasons:
1. The serializer is overkill. I shouldn't need to invoke the django rest framework to do what I am doing. Why not stick with django forms until I need something more powerful?
2. I am doing so much processing on the Destiny 2 end points, that I can't even just apply the serializer to the output of `requests`. This may not be obvious from the simple example at my project.
3. Forms are awesome and I'm not even using them. Once I make some fields unique, calling `serializer.save()` just triggers a validation error. OTOH, using forms, you can feed it an instance to simply *update* the db row without having to do a lot of extra work. Forms have lots of cool stuff like this so it isn't clear, again, why a serializer from DRF is that helpful.
On the other hand, I am a django noob, so maybe there are benefits to pulling data in via a serializer that I don't appreciate. So I'm asking for advice: when pulling data from an api into a db (using `requests`), should I use a serializer, or a form? If a form, which I'm starting to think is the right answer, I'm a little confused about why the project I copied, from UltimateDjango, even used a serializer?
What conditions for that embedly api consumer made it smarter in his case, but not my case?
-----
Here is the `save_user` function I'm using, which contains the main logic:
def add_user(save_user_form):
"""
Validate form data containing 'display_name', request their info from server,
and pull in via a serializer. Returns dictionary with two keys:
'flag': 1 if successful, 0 if not, and None if that user is not a D2 player in PS4.
'message': message about the outcome (success, validation error, etc).
"""
add_result = {'flag': 0, 'message': ''}
if save_user_form.is_valid():
user_name = save_user_form.cleaned_data['display_name']
search_url = search_destiny_player_url(user_name)
search_response = requests.get(search_url, headers = {"X-API-Key": D2_KEY})
user_data = search_response.json()['Response']
if user_data:
submission_data = {'display_name': user_data[0]['displayName'], 'user_id': user_data[0]['membershipId']}
try:
serializer = SearchPlayerSerializer(data = submission_data)
print(repr(serializer)) #for debu
**tl;dr**
When pulling data into my django database from an api, should I use forms, or a [django rest framework](http://www.django-rest-framework.org/) serializer?
**What I did**
Based on some discussion in [another thread](https://www.reddit.com/r/django/comments/7e0olm/how_to_save_json_api_response_in_database/), I built an ultra-simple example of how to extract data from an api, in this case the api for the video game Destiny 2:
https://github.com/cortical-iv/restit
Note this is essentially a port of UltimateDjango's [How to Consume REST APIs with Django & Python Requests](https://ultimatedjango.com/blog/how-to-consume-rest-apis-with-django-python-reques/).
My goal was to follow Section 12.1 of Two Scoops, and *Validate all incoming data with django forms.* But what I actually did was a two step process that I'm now thinking was sort of dumb (I pasted the code below the fold):
1. Bind data about path parameters in the url into a form.
2. After extracting the needed data from the response to `requests`, use a django rest framework *serializer* to validate and save that data.
Now that this is up and working, I realize I didn't actually really follow the advice from Two Scoops of importing through forms. I imported required *path* information (user name) through a form, but then did all the heavy lifting with the serializer. I now think it would be better to replace step 2 with another form (a SaveUser form) that itself includes all the fields I need in my database.
**Why I don't like what I did**
Why am I thinking my approach smells? A couple of reasons:
1. The serializer is overkill. I shouldn't need to invoke the django rest framework to do what I am doing. Why not stick with django forms until I need something more powerful?
2. I am doing so much processing on the Destiny 2 end points, that I can't even just apply the serializer to the output of `requests`. This may not be obvious from the simple example at my project.
3. Forms are awesome and I'm not even using them. Once I make some fields unique, calling `serializer.save()` just triggers a validation error. OTOH, using forms, you can feed it an instance to simply *update* the db row without having to do a lot of extra work. Forms have lots of cool stuff like this so it isn't clear, again, why a serializer from DRF is that helpful.
On the other hand, I am a django noob, so maybe there are benefits to pulling data in via a serializer that I don't appreciate. So I'm asking for advice: when pulling data from an api into a db (using `requests`), should I use a serializer, or a form? If a form, which I'm starting to think is the right answer, I'm a little confused about why the project I copied, from UltimateDjango, even used a serializer?
What conditions for that embedly api consumer made it smarter in his case, but not my case?
-----
Here is the `save_user` function I'm using, which contains the main logic:
def add_user(save_user_form):
"""
Validate form data containing 'display_name', request their info from server,
and pull in via a serializer. Returns dictionary with two keys:
'flag': 1 if successful, 0 if not, and None if that user is not a D2 player in PS4.
'message': message about the outcome (success, validation error, etc).
"""
add_result = {'flag': 0, 'message': ''}
if save_user_form.is_valid():
user_name = save_user_form.cleaned_data['display_name']
search_url = search_destiny_player_url(user_name)
search_response = requests.get(search_url, headers = {"X-API-Key": D2_KEY})
user_data = search_response.json()['Response']
if user_data:
submission_data = {'display_name': user_data[0]['displayName'], 'user_id': user_data[0]['membershipId']}
try:
serializer = SearchPlayerSerializer(data = submission_data)
print(repr(serializer)) #for debu
www.django-rest-framework.org
Home - Django REST framework
Django, API, REST, Home
Cell not executing
I'm really new to Jupyter Notebook. I just started using it for a class assignment we had to complete for this week. Yesterday, I started having trouble executing one of the cells in my notebook. As far as I can tell it doesn't finish executing and just has the [*] symbol next to the In for that cell. I was able to run it all week, but I'm only now having problems. I've reset my kernel many times and restarted my computer. I'm not sure what else to try. Any advice is appreciated! Here's the code for that cell if it helps:
#adding additional empty column using emptydata array created above
#initializing Severity Label array
SevLbl=np.empty([row,1])
#for loop to categorize RUL values and store labels in SevLbl
for x in range(0,row):
while df['RUL'][x]<50:
SevLbl[x]=[0]
while 125>df['RUL'][x]>=50:
SevLbl[x]=[1]
while 200>df['RUL'][x]>=125:
SevLbl[x]=[2]
while df['RUL'][x]>=200:
SevLbl[x]=[3]
df['SeverityLabel']=SevLbl
#preview new data frame below
df.head(3)
/r/IPython
https://redd.it/7qf80w
I'm really new to Jupyter Notebook. I just started using it for a class assignment we had to complete for this week. Yesterday, I started having trouble executing one of the cells in my notebook. As far as I can tell it doesn't finish executing and just has the [*] symbol next to the In for that cell. I was able to run it all week, but I'm only now having problems. I've reset my kernel many times and restarted my computer. I'm not sure what else to try. Any advice is appreciated! Here's the code for that cell if it helps:
#adding additional empty column using emptydata array created above
#initializing Severity Label array
SevLbl=np.empty([row,1])
#for loop to categorize RUL values and store labels in SevLbl
for x in range(0,row):
while df['RUL'][x]<50:
SevLbl[x]=[0]
while 125>df['RUL'][x]>=50:
SevLbl[x]=[1]
while 200>df['RUL'][x]>=125:
SevLbl[x]=[2]
while df['RUL'][x]>=200:
SevLbl[x]=[3]
df['SeverityLabel']=SevLbl
#preview new data frame below
df.head(3)
/r/IPython
https://redd.it/7qf80w
reddit
Cell not executing • r/IPython
I'm really new to Jupyter Notebook. I just started using it for a class assignment we had to complete for this week. Yesterday, I started having...