Using django 1.0.1 on windows xp and postgres database
I have a very strange problem in the Django admin page. Using the model and admin below, the field "balance" does populate with objects from the Balance model. However, it does NOT populate the locationparameter field: the name "locationparameter" does appear, but there is no pull-down menu.
History: the Balance, BalanceMember object is new and I just did a manage.py syncdb. LocationParameter did already exist in the system.
Manually creating a BalanceMember does work:
bm = BalanceMember.objects.create(balance=b, locationparameter=lp, type=1, function=1)
bm.save()
Anyone an idea where to look?
models.py:
class BalanceMember(models.Model):
balance = models.ForeignKey(Balance)
locationparameter = models.ForeignKey(LocationParameter)
type = models.IntegerField()
function = models.IntegerField()
admin.py
admin.site.register(BalanceMember)
try adding:
admin.site.register(LocationParameter)
Related
After customizing my user model in Django Oscar, I received the following error message:
IntegrityError at /
insert or update on table "basket_basket" violates foreign key constraint "basket_basket_owner_id_74ddb970811da304_fk_auth_user_id"
DETAIL: Key (owner_id)=(5) is not present in table "auth_user".
To customize my user model, I followed the instructions here.
First, I wrote the following models.py file, located within my project directory at apps/user/models.py.
from django.db import models
from oscar.apps.customer.abstract_models import AbstractUser
from django.contrib.postgres.fields import ArrayField
class User(AbstractUser):
acct_bal = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
purchased_items = ArrayField(models.IntegerField(), default=list)
The idea is that I want the user to have an account balance (which I will use for payment later) as well as a list of product numbers representing items that have already been purchased.
After making models.py, I edited the installed apps as follows:
INSTALLED_APPS = [...
'shopworld.apps.user',
] + get_core_apps()
And then put this at the bottom of my settings.py:
AUTH_USER_MODEL = 'user.User'
I then did ./manage.py migrate, but for some reason I am getting this error message. I also tried dropping the django_admin_log table as suggested here, but it did not work. Any help would be greatly appreciated.
I fixed this - the issue was that I was trying to migrate to a custom user model after already having done migrations with auth_user. This meant that auth_user didn't update correctly. I had to flush and re-sync the database, so that the initial migration captured the custom user model.
I have a model created in Django 1.5 as below:
class Number(models.Model):
phone_number = models.CharField("Phone Number", max_length=10, unique=True)
I set up Django admin as below:
from django.contrib import admin
from demo.models import Message, Number, Relationship, SmsLog
class NumberAdmin(admin.ModelAdmin):
search_fields = ['phone_number']
admin.site.register(Number, NumberAdmin)
I believe Django add "date_created" column to the database automatically (because I know it sorts the data entries by creation time in admin console). Is there a way to view those time/dates in admin console? The closest I have go to is Django tutorial and StackOverflow ,but I do not want to create another column on my own (pub_date in Django official tutorial's example) and add it if possible. Is there a way to do it and if so, could someone show me how to? Thank you!
Django does not automatically add a date_created column. If you want to track the creation date, you have to declare it in your model.
You may be getting the illusion that it does because if you do not specify a sort order in the model or in the admin class for the model, it will default to sorting by primary key, which will increase according to the order the model instances were created.
Currently programming in Python Django 1.4. In my files I have written CREATE TABLE functions for productos and clientes to be created in the MySQL database. When I checked with python manage.py sqlall ventas (ventas being the parent directory of productos and clientes), it outputs that snippet of code. However, when I tried to access them under localhost admin, I got the
1146, "Table 'demo.ventas_cliente' doesn't exist" error. And these 2 tables do now show up in MySQL.
Initially I had dropped these 2 tables because there were some DB errors. I ran syncdb again but does not seem to retrieve those 2 tables. What seems to be wrong?
I strongly suggest you don't type MySql commands directly, this may throw you many errors in the future. To create tables in Django you must create models. For example:
1) In ./Yourproject/apps/yourDBName create __ init __.py
2) in settings.py under "installed apps" add your new app. Example: yourproject.apps.yourapp
4) execute "python manage.py runserver"
5) create models.py into your new app
6) now you can create your models like that:
class myTable(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField()
7) if you want to access your new table from the admin, create admin.py in your app and type:
from django.contrib import admin
from models import myTable
admin.site.register(myTable)
#here, you can add more tables to the admin in the future
also, if you need to use foreign keys:
class parentTable(models.Model):
idParent = models.AutoField(primary_key=True)
parentName = models.CharField(null=False)
class childTable(models.Model):
idChild = models.AutoField(primary_key=True)
MyParentName = models.ForeignKey(parentTable, to_field='parentName')
childName = models.CharField(null=False)
MORE INFO: https://docs.djangoproject.com/en/dev/topics/db/models/
I am trying to represent a ManyToMany relation in the Admin panel for the following models:
models.py
class Adress(models.Model):
address = models.TextField()
class Person(models.Model):
locations = models.ManyToManyField(Address)
admin.py
class PeronsAddressRelation(admin.TabularInline):
model = models.Person.locations.through
class PersonInline(admin.TabularInline):
model = models.Person
inlines = [PeronsAddressRelation]
The problem now is that I have many relations so when loading the admin edit page of a Person, it takes a lot of time until the Admin view builds the select box of PersonAddressRelation .
Is there anyway I can show inside of PersonAddressInline the name of the address without it being inside a select box? (just static text with a Delete option).
Check ModelAdmin.raw_id_fields. Using raw_id_fields will tell django admin not to create drop down menu.
edit: I wasn't clear before, I am saving my object in the django admin panel, not in a view. Even when I save the object with no many-to-many relationships I still get the error.
I have a model called TogglDetails that has a ForeignKey relationship with the standard django User model and a MayToManyField relationship with a model named Tag. I have registered my models with django admin but when I try to save a TogglDetails instance I get the error in the title.
Here are my models:
class Tag(models.Model):
name = models.CharField(max_length=30)
def __unicode__(self):
return self.name
class TogglDetails(models.Model):
token = models.CharField(max_length=100)
user = models.ForeignKey(User)
tags = models.ManyToManyField(Tag, blank=True, null=True)
def __unicode__(self):
return self.user.username
class Meta:
verbose_name_plural = "toggl details"
As far as I can tell, there should be no issues with my models and django admin should just save the instance without any issues. Is there something obvious that I have missed?
I am using Django 1.3
The answer to my question was this: Postgres sequences without an 'owned by' attribute do not return an id in Django 1.3
The sequences in my postgres database did not have the "Owned by" attribute set and so did not return an id when a new entry was saved to the db.
As stated by other users:
Postgres sequences without an 'owned by' attribute do not return an id in Django 1.3
The sequences in my postgres database did not have the "Owned by" attribute set and so did not return an id when a new entry was saved to the db
In addition:
This is most likely caused by a backwards incompatible change that renders some primary key types in custom models beyond reach for Django 1.3. See Django trac tickets https://code.djangoproject.com/ticket/13295 and http://code.djangoproject.com/ticket/15682 for more information.
I solved the problem by running the follow commands for the affected tables/sequences.
Specifically running the command:
manage.py dbshell
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
change tablename_colname_seq and tablename.colname
Don't let us guess and add the Error message to your question, this gives most information about where it fails.
Have you imported the User model?
from django.contrib.auth.models import User
I've had this problem as well and the only thing I could do was make the M2M fields blank and not set them until I hit Save and Continue Editing.
I think this just may be a framework wart, as you will notice the User section of the Admin site also has a very strict "You can only edit these fields until you save the model".
So my recommendation is to adopt that scheme, and hide the M2M form field until the model has a Primary Key.
I tried Django 1.3 using CPython, with different database setups. I copy-pasted the models from the question, and did some changes: first I added
from django.contrib.auth.models import User
at the top of the file and I put the reference to Tag between quotes. That shouldn't make any difference. Further, I created the following admin.py:
from django.contrib import admin
import models
admin.site.register(models.Tag)
admin.site.register(models.TogglDetails)
For Sqlite3, the problem described doesn't occur, neither for MySQL. So I tried PostgreSQL, with the postgresql_psycopg2 back end. Same thing: I can't reproduce the error.
So as far as I can figure, there's nothing wrong with the code in the question. The problem must be elsewhere.