Related
We use djangoql for easy search in our django admin panel.
The mixin DjangoQLSearchMixin has been added to some of our models in the admin panel.
And sometimes after deployment we get an error in the handler
application_name/model_name/introspect/
Error:
FieldDoesNotExist at /admin/user/user/introspect/
Model_name has no field named 'field_name'
After the reboot, the error disappears. The error cannot be reproduced locally.
Example:
"Address has no field named 'membership_requests'"
#admin.register(MembershipRequest, site=admin_site)
class MembershipRequestAdmin(DjangoQLSearchMixin, admin.ModelAdmin):
list_display = ("company", "user", "request_type", "status", "created_on", "updated_on")
class MembershipRequest(PureModelMixin):
company = models.ForeignKey("constrafor.Company", on_delete=models.CASCADE, related_name="membership_requests")
user = models.ForeignKey("user.User", on_delete=models.CASCADE, related_name="membership_requests")
address = models.OneToOneField(
"constrafor.Address",
related_name="membership_requests",
on_delete=models.CASCADE,
null=True,
blank=True,
help_text="FK to constrafor.Address",
)
code = models.ForeignKey(
"constrafor.Code", on_delete=models.SET_NULL, related_name="membership_requests", blank=True, null=True
)
company_name = models.CharField(null=True, blank=True, max_length=1000)
company_phone = models.CharField(null=True, blank=True, max_length=15)
company_type = models.CharField(max_length=15, choices=Company.COMPANY_TYPE_CHOICES)
is_needed_email_verification = models.BooleanField(default=False)
status = models.CharField(
max_length=8,
choices=MembershipRequestStatus.choices,
default=MembershipRequestStatus.pending,
)
request_type = models.CharField(
max_length=10,
choices=MembershipRequestType.choices,
default=MembershipRequestType.natural,
)
As I remarked in an earlier comment on your question, this seems to be a very tricky heisenbug. Not being able to properly debug it, as it cannot be reproduced, I found a way around it:
class CustomDjangoQLSchema(DjangoQLSchema):
def get_field_instance(self, model, field_name):
"""Some obscure heisenbug caused introspect requests to raise, rendering DjangoQL useless.`
This catch the exception and just skip the problematic field.
"""
try:
return super().get_field_instance(model, field_name)
except FieldDoesNotExist:
return None
If you use this Schema instead of the default you should be able to skip those failing fields.
UPDATE: Later I found out that the model that caused trouble was not registered with Django properly. It makes sense that a model that is not imported in models.__init__ but that does have a referral causes unexpected behaviour like this.
Django 1.11 with PostgreSQL.
I go to migrate my site and models.py throws the error that I can't have more than one primary key. I can't see where I do (or I'm not understanding how).
class Employee(models.Model):
Aegis_ID = models.UUIDField(primary_key=True, null=False, default=uuid.uuid4, editable=False, serialize=True)
Employee_Number = models.ForeignKey('self', on_delete=models.CASCADE, related_name='Company_Employee_Number',
null=True, blank=True, max_length=6, help_text="Employee ID")
Employee_FName = models.CharField(null=True, blank=True, max_length=25, help_text="First Name")
Employee_LName = models.CharField(null=True, blank=True, max_length=25, help_text="Last Name")
Employee_Email = models.EmailField(max_length=80, blank=True, help_text="GPM Email address")
Employee_Position = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True,
related_name='Department_Employee_Position', max_length=3,
choices=EMPLOYEE_POSITION, help_text="Select position of this Employee.")
Hire_Date = models.DateField(null=True, blank=True, help_text="Enter the employee hire date.")
Employee_Division = models.CharField(max_length=2, null=True, blank=True, choices=DIVISION_CHOICES,
help_text="Assign the Audit Division for this employee.")
Employee_Region = models.CharField(max_length=3, null=True, blank=True, choices=REGION_CHOICES,
help_text="Assign the Audit Region for this employee.")
Employee_District = models.CharField(max_length=3, null=True, blank=True, choices=DISTRICT_CHOICES,
help_text="Assign the Audit District for this Employee.")
Reading the Django pages on this exact topic, it's listed as a problem resolved in 1.7 and had to do with how Django sorted the tables by class, alphabetically.
I've also tried python manage.py flush followed by makemigrations prior to migrate
So, what fields is Django / Postgres attempting to make an "id" and "primary" because I'm just not understanding, here...
According to the Django documentation regarding Automatic Primary Keys, there's the unseen is id = models.AutoField(primary_key=True) but I also understood that if you assign the primary_key=True to a field, this did not apply
In your above model Multiple primary keys for table “app_employee” are not allowed.
It is not coming because you have
Aegis_ID = models.UUIDField(primary_key=True, null=False, default=uuid.uuid4, editable=False, serialize=True)
Because in django documentation it is clearly specified that
Django Documentation
Field.primary_key
If True, this field is the primary key for the model.
If you don’t specify primary_key=True for any field in your model, Django will automatically add an AutoField to hold the primary key, so you don’t need to set primary_key=True on any of your fields unless you want to override the default primary-key behaviour.
primary_key=True implies null=False and unique=True. Only one primary key is allowed on an object.
I have tried your model on my project and it is working absolutely fine.
For simplicity I removed other fields
models.py
from __future__ import unicode_literals
from django.db import models
import uuid
class Employee(models.Model):
Aegis_ID = models.UUIDField(primary_key=True, null=False,default=uuid.uuid4, editable=False, serialize=True)
Employee_Number = models.ForeignKey('self', on_delete=models.CASCADE, related_name='Company_Employee_Number',
null=True, blank=True, max_length=6, help_text="Employee ID")
Employee_FName = models.CharField(null=True, blank=True, max_length=25, help_text="First Name")
Employee_LName = models.CharField(null=True, blank=True, max_length=25, help_text="Last Name")
Employee_Email = models.EmailField(max_length=80, blank=True, help_text="GPM Email address")
and when I did
(venv) astikanand#Developer-PC:~/firstsite$ python manage.py makemigrations
Migrations for 'employee':
employee/migrations/0001_initial.py
- Create model Employee
and then
(venv) astikanand#Developer-PC:~/firstsite$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, employee, sessions
Running migrations:
Applying employee.0001_initial... OK
so it is working fine.
You need to do is
Either you recreate your app or simply start your project all over again, may be some dependency issues or something. But your code for model Employee is all ok.
It is quiet possible that you changed primary keys and/or references to other models/tables and some legacy dependencies remained in the migration files.
Please refer to the official Django documentation for reverting past migrations. You do not have to restart your project to remove dependencies.
python manage.py makemigrations --empty yourappname
That is it. Then check 0001_initial.py file under migrations folder in your app to make sure all dependencies have been removed. It should look like:
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('yourappname', '0001_initial'),
]
operations = [
]
I am trying to export all my database with a prefetch_related but I only get data from the main model.
My models:
class GvtCompoModel(models.Model):
gvtCompo= models.CharField(max_length=1000, blank=False, null=False)
...
class ActsIdsModel(models.Model):
year = models.IntegerField(max_length=4, blank=False, null=False)
...
class RespProposModel(models.Model):
respPropos=models.CharField(max_length=50, unique=True)
nationResp = models.ForeignKey('NationRespModel', blank=True, null=True, default=None)
nationalPartyResp = models.ForeignKey('NationalPartyRespModel', blank=True, null=True, default=None)
euGroupResp = models.ForeignKey('EUGroupRespModel', blank=True, null=True, default=None)
class ActsInfoModel(models.Model):
#id of the act
actId = models.OneToOneField(ActsIdsModel, primary_key=True)
respProposId1=models.ForeignKey('RespProposModel', related_name='respProposId1', blank=True, null=True, default=None)
respProposId2=models.ForeignKey('RespProposModel', related_name='respProposId2', blank=True, null=True, default=None)
respProposId3=models.ForeignKey('RespProposModel', related_name='respProposId3', blank=True, null=True, default=None)
gvtCompo= models.ManyToManyField(GvtCompoModel)
My view:
dumpDB=ActsInfoModel.objects.all().prefetch_related("actId", "respProposId1", "respProposId2", "respProposId3", "gvtCompo")
for act in dumpDB.values():
for field in act:
print "dumpDB field", field
When I display "field", I see the fields from ActsInfoModel ONLY, the starting model. Is it normal?
You haven't understood the arguments to prefetch_related. It's not a list of fields, but a list of models.
(Note that your field naming convention is also very misleading - respProposId1 and actId are not IDs, but actual instances of the models. Django has created an underlying field in each case by appending _id, so the db columns are respProposId1_id and actId_id. You should just call the fields resp_propos1 and resp_propos2 - also note that normal style is lower_case_with_underscore, not capWords.)
It is normal, that you are seeing fields from ActsInfoModel only. You can access related models via dot notation, like:
acts = ActsInfoModel.objects.all().prefetch_related("actId", "respProposId1", "respProposId2", "respProposId3", "gvtCompo")
for act in acts:
print act.respProposId1.respPropos
Related models are already prefetched, so it won't produce any additional queries. FYI, quote from docs:
Returns a QuerySet that will automatically retrieve, in a single
batch, related objects for each of the specified lookups.
This is are my models i want to relate. i want for collection to appear in the form of occurrence.
class Collection(models.Model):
id = models.AutoField(primary_key=True, null=True)
code = models.CharField(max_length=100, null=True, blank=True)
address = models.CharField(max_length=100, null=True, blank=True)
collection_name = models.CharField(max_length=100)
def __unicode__(self):
return self.collection_name
class Meta:
db_table = u'collection'
ordering = ('collection_name',)
class Occurrence(models.Model):
id = models.AutoField(primary_key=True, null=True)
reference = models.IntegerField(null=True, blank=True, editable=False)
collection = models.ForeignKey(Collection, null=True, blank=True, unique=True),
modified = models.DateTimeField(null=True, blank=True, auto_now=True)
class Meta:
db_table = u'occurrence'
Every time i go to check the Occurrence object i get this error
TemplateSyntaxError at /admin/hotiapp/occurrence/
Caught an exception while rendering: column occurrence.collection_id does not exist
LINE 1: ...LECT "occurrence"."id", "occurrence"."reference", "occurrenc..
And every time i try to add a new occurrence object i get this error
ProgrammingError at /admin/hotiapp/occurrence/add/
column occurrence.collection_id does not exist
LINE 1: SELECT (1) AS "a" FROM "occurrence" WHERE "occurrence"."coll...
What am i doing wrong? or how does ForeignKey works?
The problem is that you have not updated your database table definition since adding the ForeignKey. syncdb doesn't do this for you, as the documentation clearly states. You need to update the SQL manually, or use a tool like South.
Are you sure you mean
collection = models.ForeignKey(Collection, null=True, blank=True, unique=True),
Nullable and Unique? This may not be possible in some databases.
Generally, the unique constraint doesn't seem to make much sense here.
Are you trying to force a 1-to-1 relationship? Use the OneToOneField. http://docs.djangoproject.com/en/1.1/ref/models/fields/#django.db.models.OneToOneField
Firstly, I know how to fix the problem, I'm just trying to understand why it's occuring. The error message:
users.profile: Reverse query name for field 'address' clashes with related field 'Address.profile'. Add a related_name a
rgument to the definition for 'address'.
And the code:
class Address(models.Model):
country = fields.CountryField(default='CA')
province = fields.CAProvinceField()
city = models.CharField(max_length=80)
postal_code = models.CharField(max_length=6)
street1 = models.CharField(max_length=80)
street2 = models.CharField(max_length=80, blank=True, null=True)
street3 = models.CharField(max_length=80, blank=True, null=True)
class Profile(Address):
user = models.ForeignKey(User, unique=True, related_name='profile')
primary_phone = models.CharField(max_length=20)
address = models.ForeignKey(Address, unique=True)
If I understand correctly, this line:
address = models.ForeignKey(Address, unique=True)
Will cause an attribute to be added to the Address class with the name profile. What's creating the other "profile" name?
What if I don't need a reverse name? Is there a way to disable it? Addresses are used for a dozen things, so most of the reverse relationships will be blank anyway.
Is there a way to copy the address fields into the model rather than having a separate table for addresses? Without Python inheritance (this doesn't make sense, and if an Model has 2 addresses, it doesn't work).
in the django docs it says:
If you'd prefer Django didn't create a backwards relation, set related_name to '+'. For example, this will ensure that the User model won't get a backwards relation to this model:
user = models.ForeignKey(User, related_name='+')
but I never tried it myself....
I'm not sure where the errant profile field is coming from… But one way to find out would be: temporary remove address = models.ForeignKey(…) from Profile, ./manage.py shell, from ... import Address then see what Address.profile will tell you.
I don't think there is any official way to inherit only the fields from some other Model without using inheritance… But you could fake it like this (where SourceModel is, eg, Address and TargetModel is, eg, Profile):
for field in SourceModel._meta.fields:
TargetModel.add_to_class(field.name, copy.deepcopy(field))
(this is coming from Django's ModelBase __new__ implementation)
I don't think it's possible to disable the reverse name.
I've just done a quick grep over the code and it doesn't look like there is any logic which will bypass setting up the related_name field on the related model.
For Example: Add just '+'
class GeneralConfiguration(models.Model):
created_at = models.DateTimeField(editable=False, default=settings.DEFAULT_DATE)
updated_at = models.DateTimeField(editable=False, default=settings.DEFAULT_DATE)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.PROTECT, related_name='+')
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.PROTECT, related_name='+')