I have a particular situation which I need help clarifying.
I have an existing Oracle table with an auto increment ID as a primary key
I am creating a django model to sync with that table so i can make use of django's ORM methods such as save(), filter() etc.
I read from the django docs the .save() method can perform both a UPDATE and INSERT depending on if the values passed to the primary key results in a True value (i.e. not a None or null).
In my table I have two columns which together will form a composite primary key.
If I specify primary_key = True on the two attributes on the django model, do I need to remove the primary key tag from oracle table?
Also, do i need to specify the unique_together to tell the django model that they are unique or will it be able to derive the index i created in the django oracle table?
Thanks.
Related
How to return all objects instances of a particular model that are being addresed as Foreign key by ANY object instance of a different model ? Lets say there is a model Item and a model ItemRequested that has foreign key relation to Item. How to print all Items that are being mentioned as foreign key in the ItemRequested table/model ? basically this is the SQL query that i want to execute in Django:
select * from backend_item where id in (select id from backend_itemrequested);
Obviously i want to avoid executing raw SQL commands from inside Django ORM
I'm not sure exactly what you're asking here. But perhaps this is what you want:
Item.objects.exclude(itemrequested=None)
I am new to python and Django as well. I am trying to use Django ORM for fetching data from database but i am unable to do this. My database has three tables :USER,INVESTMENT_NAME and WALLET. The columns in user table are id(primary key) and name, columns in investment_name table is id(primary key) and name and the wallet table has user_id(foreign key references id in user table) , inv_id(foreign key references id in investment_name table) ,date, quantity,amount and current price.
I am having trouble to fetch data which displays user name ,investment name,date,quantity,amount,current price using DJANGO ORM as i dont know how to do for multiple tables.
Any suggestions please.?
Do the Django ORM query on the Wallet table. Since there is a foreign key reference to both the other tables in this table, you can then access the entities using getters. Eg -
w = Wallet.objects.all()
w[0].user.name # This is the username
w[0].investment.name # This is the investment name
w[0].date # This is the wallet date
Use the Django's official documentation for more understanding on orm queries and how you can optimize it.
You should use doulble underscore to get table's ForeignKey (One-To-Many) relationship fields and related_name (field_name_set by default) to access to Many-To-One (ForeignKey that references the table) objects. There are docs: Lookups that span relationships
It's also good for perfomance to use select_related and prefetch_related.
I have a PostgreSQL database that is being used by a front-end application built with Django, but being populated by a scraping tool in Node.js. I have made a sequence that I want to use across two different tables/entities, which can be accessed by a function (nexval(serial)) and is called on every insert. This is not the primary key for these tables, but simply a way to maintain order through some metadata. Using it in Node.js during the insertion of the data into the tables is trivial, as I am using raw SQL queries. However, I am struggling with how to represent this using Django models. There does not seem to be any way to associate this Postgres function with a model's field.
Question:
Is there a way to use a Postgres function as the default value of a Django model field?
you can also set your own function for the default
from django.db import connection, models
def sequence_id():
with connection.cursor() as cursor:
cursor.execute("""SELECT nextval('model_someid_seq')""")
return cursor.fetchone()[0]
class MyModel(models.Model):
field_id = models.IntegerField(default=sequence_id)
My eventual solution:
Override the save method of the model, using a raw query to SELECT nextval('serial') inside the override, setting that as the value of the necessary field, then call save on the parent (super(PARENT, self).save()).
I have an existing MySQL database that I set up on PMA, it has FKs that references columns that are not primary keys. Now I am trying to move the database to Django and am having trouble because when I try to set up d Foreign Keys in django it automatically references the Primary Key of the table that I am attempting to reference so the data doesnt match because column A and column B do not contain the same info. Is there a way to tell django what column to reference?
You can use the to_field attribute of a ForeignKey.
Django should detect this automatically if you use ./manage.py inspectdb, though.
I'm using a hand built (Postgres) database with Django. With "inspectdb" I was able to automatically create a model for it. The problem is that some tables have multiple primary keys (for many-to-many relations) and they are not accessible via Django.
What's the best way to access these tables?
There is no way to use composite primary keys in Django's ORM as of now (up to v1.0.2).
I can only think of three solutions/workarounds:
There is a fork of django with a composite pk patch at github that you might want to try.
You could use SQLAlchemy together with Django.
You have to add a single field primary key field to those tables.
Django does have support for many-to-many relationships. If you want to use a helper table to manage this relationships, the ManyToManyField takes a through argument which specifies the table to use. You can't model anything terribly complex this way, but it is good for most simple applications.