SQLite - Joining Tables using Django - django

I am not a coder by any means but I have to do a project for college and I am really stuck, any help would be greatly appreciated.
I am using Django and SQLite. I want to join the auth_user table with an input table that I created. I have run the following SQL join which has given me the result I want.(Which is that every user has there own waist measurement) But my issue is I don't know how to implement this into Django?
This is my SQL statement:
Sql
Please Help!

Django has its own ORM (Making queries | Django docs). So that you specify the table structure with the models. I'll give you an example
models.py
from django.db import models
class AuthUser(models.Model):
first_name = models.CharField(
max_lenth=250
)
class ProjectInput(models.Model):
auth_user = models.ForeignKey(to='AuthUser', on_delete=models.CASCADE)
waist = models.PositiveIntegerField()
so you create AuthUser model and ProjectInput model and you have foreign key relation between them (Models | Django docs).
so with this models you can create the query in Django ORM like
full_name_waist = ProjectInput.objects.values_list('auth_user__full_name', 'waist')
these are the very basics od Django. I suggest you reading tutorials in the django - Getting Started | Django docs.
more sources:
Django ORM - Full Stack Python

Related

Django - PostgreSQL doesn't match default attribute

I have the following model at Django:
class Community(models.Model):
name = models.CharField(max_length=255)
members = models.ManyToManyField(User, through='Membership')
date_created = models.DateTimeField(auto_now_add=True)
But when I check the structure of the table (using Postico for PostgreSQL) the field of date_created after applying the migrations shows no default.
I have also tried with explicitly default=date.today() but it does not work.
Any ideas what I am missing?
Thanks,
Pablo
EDIT
Great thanks to this post: How to make a script to insert data in my default sqlite3 database django
I was trying to populate the database via script using PostgreSQL driver, when it is way simpler importing the Django models a use the create method (also thanks to Daniel Roseman in the comments that led me find the post).

Django - Model.py

Just a simple question.
After I connect my django app to a remote database, I don't need to use Model.py to create tables in the database, then what is the function for Model.py at that moment?
If you want to use the Django ORM, you'll need to create models in the models.py file that match your remote database. If you don't want django creating or deleting tables on this DB, the managed=False option needs to be set for each model.
https://docs.djangoproject.com/en/1.11/ref/models/options/#managed
As you said after running migrations all tables in models.py file will be created. Later on, if you want to do some database operations, you may be using Django ORM. If you don't have models.py you won't be able to do such operations.
For example:
To create an entry to the table MyModel.
from your_app.models import MyModel
MyModel.objects.create(<field_name>=<value>)
I hope this gives you some idea.

IntegrityError after customizing user model

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.

Create a django project from an existing database

I have a n existing MySQL database. I want to create a new django application with that database.
I read that I need to run "syncdb" each and every time when add a new model in django. That time adding a new table in database in <database_name>.<table_name> format. And fetching data from that table.
What is the correct method to fetch data from an existing database in django ?
This is my model:
from django.db import models
class Users(models.Model):
employee_id = models.CharField(max_length=30)
def __unicode__(self):
return self.employee_id
Use the Django model meta options to set db_table and db_column on your Models and Fields respectively. See these links for more info on how to use them:
https://docs.djangoproject.com/en/dev/ref/models/options/#db-table
https://docs.djangoproject.com/en/dev/ref/models/fields/#db-column

Django database error on one to many relationship

I created a data model in Django which has many to one relation (N topics to 1 user) like this:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Topic(models.Model):
content = models.CharField(max_length=2000)
pub_date = models.DateTimeField('date published')
author = models.ForeignKey(User)
When I try to load the data model in the admin page, I get this error:
Exception Value:
no such column: talk_comment.author_id
Did I miss something in the data model?
Thanks.
You forgot to actually modify/create the tables in database (manually, with South or manage.py syncdb).
You can't modify the table with syncdb . you need to use South Migrations
Its really very good and you can even revert back to previous migration in case of some problem