Django not reading all columns from table - python

This is a strange one. Maybe I missed it but I have search through all Django documentation and SF but could not find an answer for this. I have a table with about 30 columns. The table looks like this...
Class Customer (models.Model):
customer_id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=35)
last_name = models.CharField(max_length=35)
middle_initial = models.CharField(max_length=2)
mail_addr1 = models.CharField(max_length=50)
mail_addr2 = models.CharField(max_length=50)
mail_city
mail_state
mail_zip
bill_addr1
bill_addr2
...
...
active_yn = models.ForeignKey('Status', models.DO_NOTHING) # <-- This one
...
...
home
mobile
The offending field is "active_yn". Django keeps spitting out an error saying that it is now a valid field.
Here's the things I am sure of:
The table definitely have this field in the correct DB, schema, table, etc
It is not the last field on the table.
inspectdb for this table is also missing this field.
I drop and re-add this column and it is still not showing.
The field is a TINYINT(3) - referencing a table Django recognized.
I am using MySQL
I have been trying to debug this for days now. Any ideas?

Thank you all for helping. I found the issue. The problem is on the DB side. I am using root user connecting directly to the DB. I have the settings file connecting with a different user with tables and fields granted to it. This table has this one field not in the grant to this user (Not my doing... urgh). Anyway, once I added this field, everything works as expected.
I appreciate all your help. Hopefully the next guy will find this useful.
Cheers!

Related

Inserting data into a Django ManyToMany field with intermediate table

I have an issue that I am unable to solve. Here is the scenario: there is a retail business which sells cigarettes. This business can have many sites, but each site does not necessarily sell the same types of cigarettes. For this reason, each store owner can browse to a web portal and select the cigarette types that their site sells – these selections will act as columns for a future table. The trouble I am having is writing the data to a database because it is a ManyToMany field. From the data entry perspective, everything appears to be working. The user is able to select what they need and, as a test, I have a HttpResponse message that returns the correct value to the POST method of the view.
The problem that I’m running into is that I don’t know how to save this data in a table because the underlying table is part of a many-to-many relationship. When I try to save the data coming from the form, I get an error:
"Cannot assign "'6565'": "CigCountColumnsMappings.site_id" must be a "Sites" instance."
I have tried many things and I just can’t seem to get this to insert. I think this has something to do with the fact that site_id is a OneToOne/ForeignKey to the Sites model.
My models.py:
class Sites(models.Model):
SITE_STATUS = (
('Open', 'Open'),
('Closed', 'Closed'),
('Maintenance', 'Maintenance')
)
id = models.CharField(max_length=20, verbose_name='Site ID', primary_key=True)
def __str__(self):
return self.id
class CigCountColumns(models.Model):
column = models.CharField(max_length=10, primary_key=True, db_column="column", verbose_name="Column")
def __str__(self):
return str(self.column)
class Meta:
ordering = ["column", ]
verbose_name = "Cigarette Count Column"
verbose_name_plural = "Cigarette Count Columns"
class CigCountColumnsMappings(models.Model):
site_id = models.OneToOneField('home.Sites', on_delete=models.CASCADE, primary_key=True, db_column="site_id", verbose_name="Site ID")
columns = models.ManyToManyField(CigCountColumns, db_column="columns", verbose_name="Cig. Count Column Mappings", blank=True)
def __str__(self):
return str(self.site_id)
class Meta:
ordering = ["site_id", ]
verbose_name = "Cig. Count Column Mapping"
verbose_name_plural = "Cig. Count Column Mappings"
My views.py:
def cigarette_columns(request):
if request.method == "POST":
this_site = request.POST['this_site']
choices = request.POST.getlist('choices')
for choice in choices:
record = CigCountColumnsMappings.objects.create(site_id=this_site, columns=choice)
record.save()
if choices:
return HttpResponse([this_site, choices])
else:
return HttpResponse([this_site, "No data entered"])
The fact that site_id has a reference to the “home.Sites” table is messing me up. I need to insert the values directly into the CigCountColumnsMappings model to map site 6565 to those particular columns but I am unable to do so. I have taken a look at “through” models and read lots of documentation on ManyToMany fields but the solution still eludes me. Many thanks in advance for any help.
First, change site_id to site. This will automatically create a field named site_id that is an integer value for the primary key.
Second, columns is a reference to CigCountColumns, but you assign it as choices which is a list. You need to create a CigCountColumns instance or get one from the database or have the id for a CigCountColumns before you create a CigCountColumnsMappings.
Just as an FYI, I was unable to resolve this issue in a pythonic Django way. I ended up using Django raw SQL queries to hit the table directly (Google: "from django.db import connection") and this resolved it. It's definitely not as "clean" as using the built-in Django methods, but it's not as dirty as completely bypassing Django and using pymysql either.

Having trouble with SQLite3 table in Django

I am learning Django and have created a table in Django using PyCharm. I entered some values in the table and after that, I added another column to the table. Now, when I attempted to makekigrations, it happened successfully but when I tried to migrate, a lot of errors appeared which mainly said that an Empty Column is being attached and so on.
After that I made a lot of tries, first by allowing Null values in that column then by commenting out the column but unsuccessfully.
Now, even if I maintain the same code in the models.py file, the same errors keep appearing.
Here is the code of models.py file:
from django.db import models
class Albums(models.Model):
# name = models.CharField(max_length=250, default=None)
artist = models.CharField(max_length=250)
duration = models.CharField(max_length=20)
# def __str__(self):
# return self.artist
class Songs(models.Model):
album = models.ForeignKey(Albums, on_delete=models.CASCADE)
name = models.CharField(max_length=250)
PS: I have tried restarting PyCharm as well.
You needed to put null=True, blank=True in there before making migrations. Because you didn't do that, you now have a bad migration file and every time you make new migrations, you're just adding a file to the migrations folder, but the bad migration file still exists and is unapplied. You need to go into your migrations folder and delete the bad migration file that is causing errors. Once you do that, you should be able to migrate successfully.

MySQL gives an "Unknown column 'user.id' in 'field list'" error using Django's automatic id

I have my User model set up with no primary key so that the automatic id will be used instead. However, when I try to access it using Django's "_set" notation when it is referenced through a foreign key:
def postDetails(request, pk)
post = Post.objects.get(pk=pk)
if post.user_set.all(): # Errors on this line
[...]
I get an error from MySQL:
OperationalError at /webApp/postDetail/42/ (1054,
"Unknown column 'user.id' in 'field list'")
What am I doing wrong? Should I be accessing it differently? Are there limitations to the automatic id?
Model for reference:
class Post(models.Model):
name = models.CharField(max_length=128)
blog = models.ForeignKey('Blog')
active = models.BooleanField(blank=True)
created_date = models.DateTimeField()
class Meta:
managed = False
db_table = 'post'
def __unicode__(self):
return self.name
class User(models.Model):
level = models.ForeignKey(Level)
post = models.ForeignKey(Post)
name = models.CharField(max_length=64)
note = models.CharField(max_length=4096)
active = models.BooleanField(blank=True, default=True)
class Meta:
managed = False
db_table = 'user'
Something else that I thought to include: I did run syncdb before running this.
EDIT : wrong answer. check comments below
The problem is that you have managed set to False. According to the documentation
If False, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means. This is the only difference when managed=False. All other aspects of model handling are exactly the same as normal. This includes
Adding an automatic primary key field to the model if you don’t declare it. To avoid confusion for later code readers, it’s recommended to specify all the columns from the database table you are modeling when using unmanaged models.
You will need to define the primary key as this is not done by default anymore.
Not 100% sure, but I think even though Django will add the id field to the model class, that field will not propagate to the DB with syncdb.
One way to try it would be to rename the existing User table, run syncdb and see if the User table is created. If not (which is likely because of the managed flag) try again with managed=True. If the id field appears in this case then my guess is you'll have to add it manually to the User table with the same parameters as the automatically created one.

IntegrityError at /***/ (1048, "Column '***' cannot be null") in python django

Hi I am getting this error because I have a field in my additional user info model (user = models.OneToOneField(User)) that I am not filling in at sign up (as I want to let the user do it later).
I wondered if there was any way to solve this problem other than allowing null field in the db?
Cheers
Try to
user = models.OneToOneField(User, null=True, blank=True)
And then recreate your db.
You can find more on https://docs.djangoproject.com/en/1.4/ref/models/fields/#null
Else you can use Proxy models:
class UserExtraInfo(User):
#here your extra fields
In this case you won`t need to create UserExtraInfo instance in same time with User.
Read more on https://docs.djangoproject.com/en/1.4/topics/db/models/#model-inheritance
Integrity error occurred basically when you define some database field as not null and pass
the null or blank value
I am assuming you are storing the value in your database by django form
so in that case you can do like
if request.method == POST: # whatever the method
get_form_obj = form.save(commit = False)
Don't forget to make change in your model user field like (null = True,blank = True)
hope this will help
For me it was depth = 1 in serializers.py, just remove this part and the request goes through. Nested serializers were causing the problem because of this (in console it was showing NestedSerializer(read_only=True):)

Django: Missing tables for profiles when converting to Postgres database

I have a problem that I hope someone with insight can aid with. My first Django project is near completion and I’m currently transitioning to a Postgres database in anticipation of deploying via Heroku. The process was going fairly smoothly until this occurred when I ran python manage.py syncdb.
django.db.utils.DatabaseError: relation “report_userprofile” does not exist
LINE 1: INSERT INTO “report_userprofile” (“user_id”, “first_name”, “…
Apparently, it did not create DB tables for the UserProfile model. I’m now getting this exception when I attempt to run the server:
Exception Type: DoesNotExist at /accounts/login/
Exception Value: Site matching query does not exist.
Among the additional apps I'm using for the project is django-profiles, which I had some issues setting up which are apparently common. The "Missing Manual" site – http://birdhouse.org/blog/2009/06/27/django-profiles/ – helped resolve those but may have led to the current problem.
I am using the signals.post_save.connect(create_profile, sender=User) recommended there. I was researching what might have gone wrong and came across this post on Google Groups and answer which states that “If you’re using a post_save signal on User you can’t do that because it results in a race condition." I’m wondering if this may be causing the issue and, obviously, what would be best to resolve it and get these tables into the new database and functioning.
Any insight into how to remedy this issue would be greatly appreciated.
This is the database model that may be causing the problem:
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True, related_name="profile")
first_name = models.CharField(max_length=25)
last_name = models.CharField(max_length=35)
email = models.EmailField()
birth_date = models.DateField(blank=True, null=True)
city = models.CharField(max_length=25)
state = models.CharField(max_length=20)
zip_code = models.CharField(max_length=10)
profile_pic = models.ImageField(upload_to='profilepictures', blank=True)
def __unicode__(self):
return " %s" % (self.user)
def get_absolute_url(self):
return ('profiles_profile_detail', (), { 'username': self.user.username })
get_absolute_url = models.permalink(get_absolute_url)
signals.post_save.connect(create_profile, sender=User)
Something seems suspicious here:
INSERT INTO “report_userprofile” (“user_id”, “first_name”, “…
Those two fields are fields on the native User model, not fields on the custom Profile model. Why would it be trying to insert those fields into your Profile table?
Is there more code you're not showin here?
Looking around, I see some interesting alternative approaches to automatically creating Profile records:
http://djangosnippets.org/snippets/500/
http://www.turnkeylinux.org/blog/django-profile
But I know that the technique you're using (listed at Birdhouse) has worked well for every Django site I've built, so I'm not particularly suspicious of that.

Categories