I'm trying to work with Django model created from a mysql database which has composite foreign keys.
My models.py goes like this.
class Make(models.Model):
idmake = models.IntegerField(primary_key=True)
make = models.CharField(max_length=20L, unique=True, blank=True)
def __unicode__(self):
return self.make
class Meta:
db_table = 'make'
class Models(models.Model):
idmodels = models.IntegerField(primary_key=True, unique=True)
make = models.ForeignKey(Make, db_column='make', to_field='make')
model = models.CharField(max_length=45L, unique=True)
resource_type = models.CharField(max_length=7L)
def __unicode__(self):
return self.model
class Meta:
db_table = 'models'
class Systems(models.Model):
idsystems = models.IntegerField(primary_key=True, unique=True)
make = models.ForeignKey(Models, null=True, db_column='make', blank=True, related_name='system_make')
model = models.ForeignKey(Models, null=True, db_column='model', to_field = 'model', blank=True, related_name='system_model')
serial_num = models.CharField(max_length=45L, blank=True)
service_tag = models.CharField(max_length=45L, blank=True)
mac = models.CharField(max_length=45L, unique=True)
Now when I try to access the make field of Systems I get a ValueError.
>>> s = Systems.objects.get(pk=1)
>>> s.model
<Models: model11>
>>> s.model.make
<Make: make1>
>>> s.make
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/django/db/models/fields/related.py", line 384, in __get__
rel_obj = qs.get(**params)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 395, in get
clone = self.filter(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 669, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 687, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1271, in add_q
can_reuse=used_aliases, force_having=force_having)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1202, in add_filter
connector)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/where.py", line 71, in add
value = obj.prepare(lookup_type, value)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/where.py", line 339, in prepare
return self.field.get_prep_lookup(lookup_type, value)
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1003, in get_prep_lookup
return super(IntegerField, self).get_prep_lookup(lookup_type, value)
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 322, in get_prep_lookup
return self.get_prep_value(value)
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 997, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'make1'
I'm not allowed to change the tables and relations in the database. I'm very new to Django and I'm unable to figure out what is the correct fix for this issue. Basically I would like to be able to get and set the make field of the Systems model directly. Can someone guide me on how I should go about doing this? My initial thoughts were that I would have to create a custom ForeignKey field.
I suspect your codes don't match your DB schema. In this line:
make = models.ForeignKey(Models, null=True, db_column='make', blank=True, related_name='system_make')
Models is supposed to be Make, isn't it? And your Systems.model has to_field = 'model', did you miss to_field='make' for Systems.make? I suggest you to drop the whole DB, run syncdb and create test data again. Then see if the error still happen.
Some more tips for your code:
as your defined to_field = 'model' and to_field='make', you'd better to consider add db_index=True for make and model fields. Otherwise query performance may be bad when your dataset is large
if you're going to set Make.make and Models.model to be unique and indexed, they seems to be qualified as primary key. Are the idmake and idmodels really necessary in your case?
primary_key=True guarantees unique. unique=True is redundant
Django conversions use singular form for model definition. I.e., use Model System, rather than Models Systems. Also, usually we use id, rather than idmake, idmodels. Those are just conversions, up to you
I discovered the answer, rather by accident. Would like to share for anyone facing similar problem.
To access make directly from Systems
>>> s = Systems.objects.get(pk=1)
>>> s.model
<Models: model11>
>>> s.model.make
<Make: make1>
>>> s.make_id
u'make1'
To set make directly from a Systems object
>>> s.make_id = Models.objects.get(model='model21').make.make
>>> s.save()
>>> s.make_id
u'make2'
Be warned. This will not work if the get method of Models returns multiple or no model objects. For example if my Models table was as:
then
>>>> Models.objects.get(model='unknown').make.make
Traceback (most recent call last):
....
MultipleObjectsReturned: get() returned more than one Models -- it returned 2!
>>> Models.objects.get(model='not known').make.make
Traceback (most recent call last):
....
DoesNotExist: Models matching query does not exist.
I fell the programmer needs to be cautious about these things.
EDIT
From comments by #ZZY to this answer and to his own answer
class Models(models.Model):
idmodels = models.IntegerField(primary_key=True, unique=True)
make = models.ForeignKey(Make, db_column='make', to_field='make')
model = models.CharField(max_length=45L, unique=True)
resource_type = models.CharField(max_length=7L)
def __unicode__(self):
return self.model
class Meta:
db_table = 'models'
unique_together = ("make", "model")
And also since model field in Models is unique, the scenario described by me should not occur in a correct table
Related
I do have two models 1) patient profile, to get patient First and last name, default id and 2) Medinfo, medical information of patient used default id of patient model as foreign key.
I am trying to use django import export for bulk upload data.. in patient profile model i can able to upload data without any trouble, but when I trying upload data in medinfo model, I am getting error. I do understand due to using foreinkey as field I am getting trouble here to import data. How to do it?
Note: pat_ID is also a primary key of Medinfo model.
class Patientprofile(models.Model):
pat_Fname = models.CharField(max_length=100)
pat_Lname = models.CharField(max_length=100, blank=True)
def __str__(self):
return str(self.id)
class MedInfo(models.Model):
# Medical Information fields
Notes = models.TextField(max_length=2000, blank=True)
pat_ID = models.OneToOneField(patientprofile, on_delete=models.CASCADE,unique=True, primary_key=True)
def __int__(self):
return self.pat_ID
Error:
Line number: 1 - 'id'
, 32.0
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\import_export\resources.py", line 661, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\import_export\resources.py", line 353, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\import_export\resources.py", line 340, in get_instance
import_id_fields = [
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\import_export\resources.py", line 341, in <listcomp>
self.fields[f] for f in self.get_import_id_fields()
KeyError: 'id'
Say, I have a model Post which has many to many relation to built-in User for liked_post
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
like = models.ManyToManyField(User, related_name='liked_posts')
All is going nice for the logic, I can add/remove the like relation nicely.
But when I introduce additional attribute save for saved_post, like this:
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
like = models.ManyToManyField(User, related_name='liked_posts')
save = models.ManyToManyField(User, related_name='saved_posts')
It always throw an error below when I'm trying to even instantiate the Post model.
ValueError: "<Post: Post object (None)>" needs to have a value for field "id" before this many-to-many relationship can be used.
Note that I did not even modify/add any custom save() method.
The error just pops up even when I'm trying to call p = Post.object.create().
From what I have known, I need to save Post first before adding any like or save relation. But I got error even from saving a Post instance.
====
Edit 1
Include a full stack trace:
Error
Traceback (most recent call last):
File "/home/rahmat/Upworks/06_Made_Gatewee/gatewee/app_post/tests/test_post_save.py", line 34, in setUp
p = Post.objects.create()
File "/home/rahmat/Upworks/06_Made_Gatewee/gatewee/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/rahmat/Upworks/06_Made_Gatewee/gatewee/venv/lib/python3.6/site-packages/django/db/models/query.py", line 422, in create
obj.save(force_insert=True, using=self.db)
File "/home/rahmat/Upworks/06_Made_Gatewee/gatewee/venv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 527, in __get__
return self.related_manager_cls(instance)
File "/home/rahmat/Upworks/06_Made_Gatewee/gatewee/venv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 840, in __init__
(instance, self.pk_field_names[self.source_field_name]))
ValueError: "<Post: Post object (None)>" needs to have a value for field "id" before this many-to-many relationship can be used.
The problem here is that the save field in Post model conflicts with the internal save method and cause this error.
Simply changed the field name, like:
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
like = models.ManyToManyField(User, related_name='liked_posts')
saved_posts = models.ManyToManyField(User, related_name='saved_posts')
I have a very odd error any time I try to access one of my models. All other models work fine.
Here is what my model looks like:
class Hop(models.Model):
name = models.CharField(max_length=100, null=True, blank=True)
aa = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True, default = 10)
b = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
type = models.CharField(max_length=100, null=True, blank=True)
description = models.CharField(max_length=600, null=True, blank=True)
user_created = models.BooleanField(default=True)
def __unicode__(self):
return self.name
When I run Hop.objects.all() I get the following error. Never seen this before.
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\models\query.py", line 250, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\models\query.py", line 274, in __iter__
self._fetch_all()
File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\models\query.py", line 72, in __iter__
for row in compiler.results_iter(results):
File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\models\sql\compiler.py", line 1044, in apply_converters
value = converter(value, expression, connection)
File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\backends\sqlite3\operations.py", line 285, in converter
return create_decimal(value).quantize(quantize_value, context=expression.output_field.context)
TypeError: argument must be int or float
Simply delete all files in the migrations folder(accept __init__.py) of that app and also delete db.sqlite3. And then make migrations again and also create superuser again after migrations.
It worked for me.
Try to run this command:
python manage.py migrate --run-syncdb
It helped me.
I think you already created a model and the migration. And inserted data according to them.
Then you changed the rule in model and tried to access one of the record you already created. In this case, you will get the errors like you said:
So the answer is, you need to update your data with new model rules
*Happy to hear that you resolved the issue.
Not sure what happened, but exporting the data and re-importing fixed the error. Must have been an invalid character on either the AA or B column.
I was converting a CharField to DecimalField and encountered this error. In one of the objects, I had a an alphabetic character. I change this field to only contain numeric characters and the change worked, I didn't have to delete any of the data or migrations.
From: ordered_quantity = models.CharField(max_length=128)
To: ordered_quantity = models.DecimalField(max_digits=100, decimal_places=4)
what could the issue with my model.py .i have tried everything nothing happens.and i think i defined my foreign key the right way .could it be a problem with my defining or do i have to use memberid.user in my foreginkey or what would be effect.any contribution is welcomed.
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x7f6a926d69b0>
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 405, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
tithe.tithe.memberid: (fields.E300) Field defines a relation with model 'memberid', which is either not installed, or is abstract.
tithe.tithe.memberid: (fields.E307) The field tithe.tithe.memberid was declared with a lazy reference to 'tithe.memberid', but app 'tithe' doesn't provide model 'memberid'.
tithe.tithe: (models.E012) 'unique_together' refers to the non-existent field 'IntegerField'.
System check identified 3 issues (0 silenced).
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x7f3d3ccdc9b0>
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "/usr/lib64/python2.7/site-packages/django/core/management/base.py", line 405, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
tithe.tithe.memberid: (fields.E300) Field defines a relation with model 'User', which is either not installed, or is abstract.
tithe.tithe.memberid: (fields.E307) The field tithe.tithe.memberid was declared with a lazy reference to 'tithe.user', but app 'tithe' doesn't provide model 'user'.
tithe.tithe: (models.E012) 'unique_together' refers to the non-existent field 'IntegerField'.
this my model.py code
from django.utils import timezone
from django.db import models
# Create your models here.
class tithe(models.Model):
memberid = models.ForeignKey('User')
membername = models.CharField(max_length=45)
receitcode = models.CharField(max_length=45)
tithes = models.IntegerField()
combinedoffering = models.IntegerField()
campmeetingoffering = models.IntegerField()
churchbuilding = models.IntegerField()
conference = models.IntegerField()
localchurch = models.IntegerField()
funds = models.IntegerField()
total = models.IntegerField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.receitcode
class Meta:
unique_together = ["receitcode","IntegerField"]
ordering = ["published_date","membername"]
The line below
memberid = models.ForeignKey('User')
is causing the problem. You have to pass a User object to it.
Import User model.
from django.contrib.auth.models import User
then
memberid = models.ForeignKey(User)
The first two warnings are because Django cannot find the model 'User' that you refer to in the memberid foreign key.
I recommend you use settings.AUTH_USER_MODEL to reference the user model. This will work whether or not you have a custom user model.
memberid = models.ForeignKey(settings.AUTH_USER_MODEL)
See the docs for more info on referencing the user model.
Note that it would be better to call name your field member. That way, the related instance will be member and the related id will be member_id. At the moment, the related instance is memberid, and the related id is memberid_id.
The final warning is because you do not have a field IntegerField in the model. If you want the receitcode field to be unique by itself, then remove the unique_together line and change the field to:
receitcode = models.CharField(max_length=45, unique=True)
Your ForeignKey needs to reference a concrete model or abstract model. Since you are referencing using a string (which means the model is abstract) you need to declare the class Meta: as abstract by stating abstract = True
Relationships defined this way on abstract models are resolved when the model is subclassed as a concrete model and are not relative to the abstract model’s app_label:
The below information is from the Django Documentation https://docs.djangoproject.com/en/1.11/ref/models/fields/
products/models.py
from django.db import models
class AbstractCar(models.Model):
manufacturer = models.ForeignKey('Manufacturer', on_delete=models.CASCADE)
# This is what you need to add
class Meta:
abstract = True
I am just exploring how to get around with Django, and i created two models in my Django app.
from django.db import models
#first model
class Person(models.Model):
name = models.CharField(max_length=40)
email = models.CharField(max_length=100)
title = models.CharField(max_length=100)
image = models.CharField(max_length=200)
def __str__(self):
return self.name
#second model
class Skill(models.Model):
person = models.ForeignKey(Person)
skill = models.CharField(max_length=60)
years = models.CharField(max_length=40)
def __str__(self):
return self.skill, self.person
The first model is Person and the second model is Skill. Now how the relation goes is that each Person will have many skills.
Now I can update the database with the data, the admin section of the site also works fine.
On the Django Shell, I try to run the command:
Skill.object.all()
and what i get is the following error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\query.py", line 235, in __repr__
return '<QuerySet %r>' % data
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\base.py", line 572, in __repr__
u = six.text_type(self)
TypeError: __str__ returned non-string (type tuple)
or if i try the command:
Skill.objects.get(pk=1)
i get:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\base.py", line 572, in __repr__
u = six.text_type(self)
TypeError: __str__ returned non-string (type tuple)
However if i run a command such as :
Skill.objects.get(skill='Photoshop').person.name
I get the name of the person who has the skill "Photoshop."
I am trying to understand what I am doing wrong here; maybe I am not supposed to query a table with the foreign key this way? Or maybe I am doing something wrong.
Well, finally what I like to query is, I want to find all the skills of a Person with a given name or primary key.
__str__ should return a str. So Change something like this
return self.skill, self.person
to
return "%s-%s" %(self.skill, self.person.name)
Your __str__ method returns a tuple (self.skill, self.person), it must return those object's str representation. In order to achieve that, change:
return self.skill, self.person
to
return "{}, {}".format(self.skill, self.person)