Slugfield in tango with django - django

I'm reading this tutorial to lear the basics of django and I'm facing a problem I can't solve.
I'm at this page http://www.tangowithdjango.com/book17/chapters/models_templates.html at the sluglify function approach.
In the tutorial the author says we have to create a new line in our category model for th slugfield. I folow strictly all the steps just as I show here:
from django.db import models
from django.template.defaultfilters import slugify
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
likes = models.IntegerField(default=0)
views = models.IntegerField(default=0)
slug = models.SlugField(unique=True)
def __unicode__(self):
return self.name
class Page(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
url = models.URLField()
views = models.IntegerField(default=0)
def __unicode__(self):
return self.title
When I run the "makemgiration" command everything works as expected: I choose the first option and provide ‘’ . BUT when I run "migrate" I get:
django.db.utils.Integrity error: Slug column is not unique
What is going on here? I've repeated several times the migrations and tried other default codes but with the same ending. I can't figure out what im doing wrong. They only thing left is that i'm giving something else instead of ‘’ (Firstly I thoughtthey were '' or ").
Thankyou for your time and help!

Delete db.sqlite3 and re run
python manage.py syncdb
Then perform the migrations
python manage.py makemigrations rango
python manage.py migrate
and re run your population script. This is a downside of Django that whenever you change models.py the db must be recreated.

I am also going through the tutorial and I was having the same issue a couple days ago.
I believe the problem is that you are trying to add a new field (slug) and have it be unique for each of the elements in your table but if I'm not mistaken you already have some data in your table for which that value does not exist and therefore the value that this field would get for those rows is not unique, hence the "django.db.utils.Integrity error: Slug column is not unique".
What I did was simply to delete the data in the table (because it was only a couple of fields, so no big deal) and THEN perform the addition of the field to the model. After doing that, you can put the data back in the table (if you're following the tutorial you should have a script for automated table population so you just run that script and you're golden). Once you have done that, all the rows in the table should have a unique slug field (since it is automatically generated based on the category name) and that solves the problem.
Note that if your table is very large, this may not be a very good solution because of the deletion of data so perhaps there is a better way, like adding that field to the model without it being unique, then running a script that sets the slug field for every row to an unique value and then setting the model's field as unique but I'm not very knowledgeable on SQL and the first solution worked just fine for me so it should work for you as well.

try deleting the sqlite3.db file from the project's directory. i was stuck on a similar problem and that really works..also even if you modify your population script, you have to delete and the recreate the db file to see the changes made....

Related

Django models migrations: error--trying to add a non-nullable field

When I makemigrations to my models, the terminal gives a warning of: "You are trying to add a non-nullable field, etc" and asked for 2 options. I must have made migrations 7 times--should I delete the "0001_initial.py", "0002_auto file" as well as the db.sqlite3? I don't need to keep the database information I inputted since I merely only did tests to see if the models worked---I just want to make sure I don't delete the database itself so I can further test my models to see if they're working. Can someone please verify the specific files I would need to delete so I can make migrations? Your help will be much appreciated!
So far my migrations folders look like this: 001_initial.py, 0002_auto file, 0003_auto file, 0004_auto_file, 0005_auto_file, 0006_auto file, and finally 007_order_buyers file----the last file concerns me--I think it's bc I must have clicked option 2. I just merely want to be able to makemigrations and I am wary of my models not working if I delete important files.
models.py
class User(models.Model):
first_name=models.CharField(max_length=100)
last_name=models.CharField(max_length=100)
email=models.CharField(max_length=100)
password=models.CharField(max_length=100)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
class Order(models.Model):
full_name=models.CharField(max_length=100)
cc_number=models.PositiveIntegerField()
exp_date=models.PositiveIntegerField()
cvc=models.PositiveIntegerField()
buyer=models.ForeignKey(User, related_name="bought_tickets", on_delete=models.PROTECT)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
class Ticket(models.Model):
venue=models.CharField(max_length=100)
quantity=models.PositiveIntegerField()
price=models.DecimalField(default=25.00, max_digits=5, decimal_places=2, null=True, blank=True)
loop=models.CharField(max_length=100)
purchaser = models.ForeignKey(User, related_name="purchases", on_delete=models.PROTECT)
order=models.ForeignKey(Order, related_name="orders", on_delete=models.PROTECT)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
Running makemigrations doesn't do anything to the database, so you can back anything out easily if you haven't run migrate. All that makemigrations does is to create a file. If you don't like it, remove it. When it gives you the error about non nullable field, the options are pretty explicit. Just read them:
You are trying to add a non-nullable field 'foo' to bar without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option:
If you select (1), the cli will next prompt you to submit a default value. After you do that, it clearly tells you that it has created a file, and tells you the name.
If you select (2), nothing happens. They're leaving it up to you to fix the problem, which you can do in the model by either making the field nullable (add null=True in the field definition) or giving it a default (default=...).
I would suggest taking a look in those files to make sure you understand what they are doing. It's all pretty straightforward. Any of them which have not been run yet can be changed. You can edit them directly, or remove and regenerate them.
To see which ones have been run, use showmigrations.

Django and computed fields in a legacy database

I am gradually replacing a legacy database front end with a django based system. All models are Managed = False, to preserve the original db structure.
However I have struck a problem where a field in a table is computed. The field is defined in (pseudo) sql as full_name = fname|| ||lname.
I can define the full_name field as a charfield; and I can read it no problems, however any attempt to update the record results in an update error on that field.
I can use a #property; but that replicates the functionality in django, rather than displaying the results from the db itself. Using this approach will cause problems with more complex fields (in tables I am yet to get to) that are defined with UDFs.
What is really needed is a 'read_only' or 'computed' attribute on the model itself; what is the best way to achieve this?
Do you just want to define a method in your class? Something like:
def Person(models.Model):
fname=models.CharField(...)
lname=models.CharField(...)
def fullname(self):
return self.fname+" "+self.lname
(not exactly sure what Managed=False means...)
if you are trying to make calculation on a database models and pass the value of a model field to another model field of the same class model, using a defined function then this solution might help you. for example lets assume you have an investment company and you give 20% per month for the capital each user invested, you would want want to pass value from capital model to a function that calculates the percentage interest, and then you will pass that function into another field monthly_payment and get saved in the database.
1) pip install django-computed-property
2) add 'computed_property' to your installed apps in project settings.
3) in your models.py, import computed_property then
class Investment(models.Model):
name = models.CharField(max_length=200)
capital = models.FloatField(null=False)
percentage = models.CharField(max_length=5)
duration = models.CharField(max_length=10, default="1 months")
monthly_payment = computed_property.ComputedFloatField( compute_from='monthly_percentage', null=False, blank=False)
then your function to perform the calculation will go thus
#property
def monthly_percentage(self):
return (20 / 100) * self.capital
Note: what i discovered was if you use the inbuilt django fields be it FloatFiled or IntegerField, this function won't read the amount you are passing in to get your 20% calculations.i hope this works for you as i stated all as they worked for me,cheers.

In Django/South HOWTO create an instance of a model from a different app during DataMigration

I need to perform a datamigration of a model Answer in app Question. In that script there is a dependency such that I need to create an instance of a model Chapter which is in the app Journal. So, I coded it as follows:
def forwards(self, orm):
for answer_object in orm.Answer.objects.all():
#This Works.
blog, is_created = orm['blog.Post'].objects.get_or_create(title=answer_object.answer[:100])
blog.save()
#This DOES NOT work
chapter, is_created = orm['journal.Chapter'].objects.get_or_create(content_object=blog)
chapter.save()
#cleanup task, not relevant to this question below
answer_object.chapter_ptr = chapter
answer_object.save()
But as expected this throws an error on " orm['journal.Chapter'].objects.get_or_create(content_object=blog)" saying that
django.core.exceptions.FieldError: Cannot resolve keyword 'content_object' into field.
This is presumably due to content_object being a GenericForeignKey so some operations are not allowed. But I also tried other alternatives for creating the "chapter" object like,
chapter = orm['journal.Chapter'](content_object=blog)
ERROR > TypeError: 'content_object' is an invalid keyword argument for this function
and
chapter = orm.journal.Chapter(content_object=blog)
ERROR > AttributeError: The model 'journal' from the app 'questions' is not available in this migration. (Did you use orm.ModelName, not orm['app.ModelName']?)
So where am I going wrong? Any pointers appreciated. Thanks.
UPDATE
So since my earlier approach was failing I tried a new tack. The model whose instantiation was failing in my code above i.e. Chapter in the Journal app, I decided to create a datamigration for that instead. I also made sure to --freeze the models I am referring to in the forwards definition. Now this should have been straight forward, I would think. I have my forward code as follows -
def forwards(self, orm):
for answer_object in orm['questions.Answer'].objects.all():
#Works, AGAIN!
blog, is_created = orm['blog.Post'].objects.get_or_create(title=answer_object.answer[:100])
blog.save()
# DOES NOT WORK, AGAIN!
chapter = orm.Chapter(rank=1, content_object=blog)
chapter.save()
I would have thought that now since I am creating instance of a model (Chapter) which exists in the subject app (Journal) everything should have worked out. But i am getting the same error.
TypeError: 'content_object' is an invalid keyword argument for this function
It fails at the same point, namely, "content_object". I will post below the model definition if that might help.
class Chapter(models.Model):
rank = models.IntegerField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
UPDATE 2
Wanted to add that all the models being touched in these forwards methods, namely - blog, chapter, questions; are fully defined in the 00n_*.py files created by South's schemamigration.
After getting help from Rob and folks on the South & Django user groups I was able to resolve this issue. Below is the definition of my forwards datamigration script.
def forwards(self, orm):
for answer_object in orm['questions.Answer'].objects.all():
blog, is_created = orm['blog.Post'].objects.get_or_create(title=answer_object.answer[:100])
blog.save()
#I have to manually lookup the content_type ans set it in the chapter creation.
ct = orm['contenttypes.ContentType'].objects.get(app_label="blog", model="post")
chapter = orm.Chapter(rank=1, content_type=ct, object_id=blog.id)
chapter.save()
This has sort of been answered before here Django South: Creating schemamigration for more than one app
Basically south doesn't currently support multiple applications.
If you don't want to entangle your applications even more than they are, I would use raw SQL in a db.execute as a quick fix.
It looks like blog and journal are very inter-related. Are you sure you want them in separate applications?
In order to create a migration via manage.py, pass --freeze=other_app as arguments to add the models definitions of this other_app to the migration itself.
This is a comment on Chantz's answer, which worked great for me (Don't have the rep to post this as a comment)
To save some others some time, you also need to freeze the contenttype app when you create your migration:
python manage.py datamigration yourapp migrationname --freeze contenttypes

Creating Custom User Backends in Django

I have a Shops model and would like each shop to be able to login to my application. Following as best I can the guide at http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/ and various other googlings, I've got part of the way there, but I've run into a problem. When I try to login as a shop, I get the following error:
OperationalError at /login/
(1054, "Unknown column 'shops.user_ptr_id' in 'field list'")
Shops model:
class Shops(User):
shop_id = models.AutoField(primary_key=True)
shop_code = models.CharField(unique=True, max_length=5)
shop_type_fk = models.ForeignKey(ShopTypes,
null=True,
db_column='shop_type_id',
blank=True)
address_fk = models.ForeignKey(Addresses, db_column='address_id')
phone_number = models.CharField(max_length=30)
#email = models.EmailField(max_length=255, blank=True)
description = models.TextField(blank=True)
does_gift_aid = models.NullBooleanField(null=True, blank=True)
objects = UserManager()
class Meta:
db_table = u'shops'
I've sync'd the database, so surely it should have made the column user_ptr_id. Does anyone know where I'm going wrong?
"I've sync'd the database, so surely it should have made the column user_ptr_id."
What makes you think that? Especially in light of this clear statement in the docs for syncdb:
Syncdb will not alter existing tables
syncdb will only create tables for
models which have not yet been
installed. It will never issue ALTER
TABLE statements to match changes made
to a model class after installation.
Changes to model classes and database
schemas often involve some form of
ambiguity and, in those cases, Django
would have to guess at the correct
changes to make. There is a risk that
critical data would be lost in the
process.
If you have made changes to a model
and wish to alter the database tables
to match, use the sql command to
display the new SQL structure and
compare that to your existing table
schema to work out the changes.
It does sound like you had an existing shops table before changing it to inherit from User (as Daniel notes), and syncdb does not update the schema for existing tables.
You need to drop the table and then run syncdb, if possible. Otherwise you need to go into your database and add the user_ptr_id field manually, if you know how to do that. The definition should look something like this:
"user_ptr_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id")

Django reading old model?

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