i am using django 1.0.4 and have 3 models: Category <-1-m-> Item <-1-m-> Image.
i recently changed the id/pk to uuid:
id = models.CharField(max_length=32, primary_key=True,
default=make_uuid, editable=False)
...
def make_uuid():
return str(uuid.uuid4()).replace('-','')
after i started using uuid, the Item's admin page would no longer let me inline add an Image. it would give me a form validation error even though there isn't any error. furthemore, if an Item has an Image, i can't change the Category, it would give me the following error:
KeyError at /admin/inventory/item/90965921681b4b69880b36bd716b0831/
id
...
/local/env/bfi2/lib/python2.6/site-packages/Django-1.0.4-py2.6.egg/django/forms/forms.py in _raw_value
213. field = self.fields[fieldname]
i think this is similar to the following bug: http://code.djangoproject.com/ticket/10992. ideally, i would like to avoid upgrading django and just patch the necessary files. has anyone else ran into this problem?
thanks,
steve
I've gone into such problems but they got solved with django-extensions UUID field.
But I cannot guarantee that this Field will work with an old django version, it was on 1.2.3
Related
Can anyone help me out here? I am inserting data into my postgreSQL DB.
admin_created is a booleanfield set to false by default. I've provided a true value for the first workout, but left the second workout blank for the booleanfield. Based on my understanding it should automatically be set to false, but i'm getting the error message below. Any ideas on why this is happening?
#.sql
INSERT INTO main_app_workout(name, description, admin_created)
VALUES
('Yoga', 'Roll up your yoga mat and discover the combination of physical and mental exercises that have hooked yoga practitioners around the globe.', 'True');
INSERT INTO main_app_workout(name, description)
VALUES
('Boxing', 'Ready to get your sweat on? Learn the six basic punches to build the foundation of an experienced boxer.');
#models.py
class Workout(Model):
name = models.CharField(max_length=40)
description = models.TextField()
exercises = ManyToManyField(Exercise, blank=True)
admin_created = models.BooleanField(default=False)
#Error code
psql:db/create_main_exercises.sql:49: ERROR: 23502: null value in column "admin_created" of relation "main_app_workout" violates not-null constraint
EDIT:
Thank you all for the comments. My solution to this problem was to provide true values for the admin_created for the seeded data. In addition I changed the admin_created field to
admin_created = models.BooleanField(null=True, default=False)
When I create new instances of the model in Django it automatically sets it to False.
Django is builded to use the ORM, if you are doing this insertion manually, Django can't set the defaul value for you, so will be passed NULL.
The field based constraints are only placed at the code level in django not at the DB level.
So if you 'll create a object in django programmatically you 'll not face this issue. But when created using SQL you 'll face this issue.
Only a few constraints are available that you can apply at the DB level that too post django 2.2.
Check the constraints documentation for django here
class Configuration(models.Model):
configuration_key = models.CharField(max_length=100, unique=True)
configuration_value = models.TextField()
configuration_description = models.TextField("a brief description of this setting", blank=True)
The above is my model. I am able to able to add configurations using the admin. I have not configured any customizations on the admin. It is a very basic
admin.site.register(Configuration)
When I update an existing configuration with admin, it throws an IntegrityError
duplicate key value violates unique constraint "config_configuration_configuration_key_key"
DETAIL: Key (configuration_key)=(SAMPLE_CONFIGURATION) already exists.
My question: Shouldn't the admin know that an existing configuration is modified and handle this accordingly? Am I missing something? To be clear - SAMPLE_CONFIGURATION - there is only one such row in the table with that configuration_key. I am trying to edit the configuration_value for that row using the admin and it throws the Integrityerror.
Your code is just fine, there must be no error. Update Django to the current version and try again.
Also try to isolate the problem. Use tests to reproduce the issue. If tests are passed, then problem not in your code.
Double check what is inside DB, try to update row directly using SQL.
This test updates configuration_value without any integrity issues.
class ConfigurationModelTest(TestCase):
def test_uniq_issue(self):
config = Configuration.objects.create(configuration_key='SAMPLE_CONFIGURATION',
configuration_value='value')
config.save()
config.configuration_value = 'updated_value'
config.save()
I am using django-1.7 in my app. I have a model which has two DateTimeFields. Like
class Task(models.Model):
start_time = models.DateTimeField(null=True, blank=True)
finish_time = models.DateTimeField(null=True, blank=True)
I want to filter the fields start_time and end_time with hours gte and lte so that i can get the queryset which contains Task objects which have to start at a particular time and have a to end at a particular time. What I tried in
Task.objects.filter(start_time__hour__gte=2)
Task.objects.filter(end_time__hour__lte=2)
But this query gives me error
FieldError: Unsupported lookup 'hour' for DateTimeField or join on the field not permitted.
I have tried queryset.raw also which provides correct values but gives error when using with django-filters package.
def start_time_filter(self, queryset, value):
if value.isdigit():
return queryset.raw("select * from app_task WHERE TIME(`start_time`) >= '{0}';".format(value))
return queryset.none()
Help will be appriciated
I'm not sure about this, but by reading the doc sounds like the hour__gte=2 syntax is only added in django 1.9. Previous versions of django only allow exact lookup like hour=2. You might consider upgrading django and try again.
Regarding the raw query error, I don't know what error did you get, but django doc also has the syntax for that. You might look up on the exact sql statement because each sql statement for a database is different. This might solve your problem is you don't want to upgrade django.
django doc about hour lookup in django 1.7
django doc about hour lookup in django 1.9
Very simple... I am using Django 1.4.1, and need to order a queryset by the inverse of the number of comments on it. I am using the Django comment framework, and have attempted to use the .annotate(comment_count = Count('comment') structure recommended in other answers... I get 'comment' doesn't resolve to a field error.
I've also tried the 0.3.1 version of django-generic-aggregate, which throws a database error, so that's out.
Photo.objects.filter(galleries=gallery).annotate(comment_count=Count('comments')).order_by('-comment_count')[(page-1)*RESULTS_PER_PAGE:page*RESULTS_PER_PAGE]
Any suggestions?
Put the function under Photo model
class Photo(models.Model):
.........
def orders(self):
//filter first the highest count of comments
aggregate = Photo.objects.aggregate(comment_count=Count('comments'))
return aggregate['comment_count'] + 1
Then you can call it like this in view:
comments = Photo.objects.filter(galleries=gallery).orders()
I changed the model, synced the db, and now when i do:
Prs = Products.objects.filter(PrName__icontains='bla')
I get the error:
ERROR: column search_products.pr_name does not exist
LINE 1: SELECT "search_products"."id", "search_products"."pr_name", ...
But pr_name was the old model, this is how the new model looks like:
class Products(models.Model):
PrName = models.CharField(max_length=255)
PrDescription = models.CharField(max_length=4000)
PrPrice = models.DecimalField(max_digits=5, decimal_places=2)
PrCompany = models.ForeignKey(Companies)
def __str__(self):
return self.PrName
Why am i getting this error? I synced the db 100 times, checked all the code, there is no reference to pr_name anywhere?
Have you tried restarting your server? If you are using anything other than the development server, you'll probably need to do that manually after making changes like this.
Unfortunately the thing you try to do is not supported by django out of the box :-(
but you can do it ether by adding a db_column to the fields or by exporting the data, removing the table from the database, edit the export file, recreate the database table and reimporting the data.
Also look at the various schema evolution solutions out there