I am using django 1.4. I have a primary key filed called city_id. when i go to add a new record in django admin, it asks me to enter the primary key. why is it doing so.
class CityMaster(models.Model):
city_id = models.IntegerField(primary_key=True)
You need a primary key. If you want Django to handle this value for you then use an AutoField.
city_id = models.AutoField(primary_key=True)
If you do so, it means no automatic key will be provided by django which is why you have to provide it by hands.
Related
For establishing many-to-many relationships, django admin has a nice widget called 'filter_horizontal'.
The problem is - it only displays primary key of the related table.
How do I add other fields from the related table?
For example, if I have a many-to-many relationship in my Order model with User model, in 'Order' django admin I can only see User's primary key(id). How do I add their name into the widget?
Turns out it is changed in str method for the User model
Hi I'm making a user model in Django but getting an error regarding email field and the unique=True also not working on the email field.
class User(models.Model):
user_name = models.CharField(max_length=50,unique=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
User_email = models.EmailField(max_length=70,blank=True,unique=True)
password = models.CharField(max_length=12)
The error I am getting when I am trying to run the command "python manage.py makemigrations":
You are trying to add a non-nullable field 'first_name' to user 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:
Basically, you already have users in your database. When you add a new field to User, there ends up being a blank spot in the database for the existing users.
However, your code is such that a blank spot is not permitted. As your code is currently, there must be a value in that spot for all users.
Your options:
1. Wipe your database
If you aren't far long in the development process, just reset your database to have no users. Then all should work properly.
2. Let it be blank/null for some users and fix it later
Add one of the following to your User_Id declaration. default='DEFAULT VALUE', blank=True or null=TrueThat will let you continue along, but then you will need to fix it later for the older users.
I will also point out that your error is not about the email (User_email)field, but rather the User_id field. Just delete that entirely. Django models already come with a built in system for managing primary keys. It is the pk.
I have a PSQL database with portfolios and their values each day for a few years.
I'm using Django to develop a web app behind this. I used this link:
https://docs.djangoproject.com/en/1.10/howto/legacy-databases/
to generate the models for Django. I did not explicitly define a primary key in my database or in the models.py file. Is it necessary to define a primary key in order to retrieve data from the DB?
If so, is there a way to make multiple fields a primary key? In this case it has to be the portfolio ID and the date, since there are multiple portfolios and dates.
Thanks for any help!
It is not necessary to define primary key in your code. By default it is taken care by django. It creates variable called id as Primary key.
However you can explicitly define primary key by using primary_key = True option.
Example.
key1 = models.IntegerField(primary_key=True)
If you want to define composite primary key then you can have a look at this question How to create composite primary key
Hope this helps !
By default, django created a column called id for each table, which is the PK.
You can use your own PK using primary_key=True.
Also, you can use unique_together Meta option, but you still need to use another column as PK.
If what you want is to use composite primary key, you need to use another thirdparty solution.
I faced the same issue and it was solved by replacing IntegerField by AutoField as below,
order_id = models.IntegerField(primary_key=True)
by
order_id = models.AutoField(primary_key=True)
And it worked !!
PostgreSQL dont make auto pk field. so you need to make it manually.
models.py
test = models.AutoField(primary_key=True)
you dont need unique=True, because pk is always unique
or you can use this, to generate a auto pk field
settings.py
DEFAULT_AUTO_FIELD='django.db.models.AutoField'
I have a django model, NewsItem, which has several fields, including date, text, and foreign key fields. One foreign key field is:
editor = models.ForeignKey(User, verbose_name="Editor", related_name='editors',
limit_choices_to=_editors)
For some reason, the foreign key fields do not show up in the "add an item" form in the django admin interface (at http://[hostname]/admin/[app name]/newsitem/add/. However, all the other fields do. I can't save any items because editor is a required field.
I have checked to make sure that there is a user satisfying the constraint:
>>> User.objects.filter(groups__name__iexact='editors')
[<User: testeditor>]
I can't find any reason that these fields wouldn't show up in the admin. Do I need to specify a widget for them in the NewsItemAdmin class? Do I need to tell the admin to display them? If so, how?
You should make sure the user you're creating this with has permission to change the editor. [facepalm]
When I inspect the Trac database I get:
class TicketChange(models.Model):
ticket = models.IntegerField()
time = models.BigIntegerField()
author = models.TextField(blank=True)
field = models.TextField()
oldvalue = models.TextField(blank=True)
newvalue = models.TextField(blank=True)
class Meta:
managed = False
db_table = 'ticket_change'
With not primary key:
>>> TicketChange.objects.all()
DatabaseError: column ticket_change.id does not exist
LINE 1: SELECT "ticket_change"."id", "ticket_change"."ticket", "tick...
Because I need to specify a pk but the original primary key of ticket_change in Trac is:
Primary key (ticket, time, field)
But It's not possible in Django: Django Multi-Column Primary Key Discussion.
If I define time like pk I can't add two tickets changes in the same time.
What can I do?
You're right. It's a known problem. So the only solutions are hacks (sort of).
Your best option is to use django-compositepks. The drawbacks are that it doesn't really support model relationships, so you will not be able to navigate to any relationship from your composite-pk model. However, looking at your TicketChange model this doesn't seem like an issue (unless you have more models with relationships to this one).
Another option would be to manually add the id column (and make sure to apply any additional changes to the db), thereby creating the new one-column primary key. A third option (and what I would probably do), pretty similar to the last one but cleaner, would be to create a new database from scratch and then populate it with a script by fetching the data from your existing legacy db.
Sorry I don't have any better solution for you, that's the way it is. Legacy dbs and django are always a headache, have gone through some similar processes myself. Hope this helps!
This is a use case the Django ORM simply does not support. If you're able to modify the database schema, add an additional column that will serve as the primary key for Django: An integer field with AUTO_INCREMENT (MySQL) or a SERIAL (PostgreSQL). It shouldn't desturb your other applications using the table since it will be managed by your database when new records are inserted. You can still use the actual primary key when making queries through the ORM:
ticket_change = TicketChange.objects.get(ticket=1234, time=5678, field='myfield')
If you’d like to specify a custom primary key, specify primary_key=True on one of your fields. If Django sees you’ve explicitly set Field.primary_key, it won’t add the automatic id column.
Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).
https://docs.djangoproject.com/en/3.1/topics/db/models/#automatic-primary-key-fields