There are tables:
comments(id, body, object_kind(photo, topic, ...), object_id),
photos(id, title),
topics(id, title, body),
...(id, ...).
How it possible link using FK object_kind+object_id with suitable row in related tables? And create onCascade=DELETE (remove comments of topic when removing topic).
One of sollution: create separate columns to each relation type: comment_id, photo_id, but I'm trying find more flexible sollution.
No way. Only separate columns like comment_id, photo_id which are nullable=true
Related
Been searching the web for a couple hours now looking for a solution but nothing quite fits what I am looking for.
I have one model (simplified):
class SimpleModel(Model):
name = CharField('Name', unique=True)
date = DateField()
amount = FloatField()
I have two dates; date_one and date_two.
I would like a single queryset with a row for each name in the Model, with each row showing:
{'name': name, 'date_one': date_one, 'date_two': date_two, 'amount_one': amount_one, 'amount_two': amount_two, 'change': amount_two - amount_one}
Reason being I would like to be able to find the rank of amount_one, amount_two, and change, using sort or filters on that single queryset.
I know I could create a list of dictionaries from two separate querysets then sort on that and get the ranks from the index values ...
but perhaps nievely I feel like there should be a DB solution using one queryset that would be faster.
union seemed promising but you cannot perform some simple operations like filter after that
I think I could perhaps split name into its own Model and generate queryset with related fields, but I'd prefer not to change the schema at this stage. Also, I only have access to sqlite.
appreciate any help!
Your current model forces you to have ONE name associated with ONE date and ONE amount. Because name is unique=True, you literally cannot have two dates associated with the same name
So if you want to be able to have several dates/amounts associated with a name, there are several ways to proceed
Idea 1: If there will only be 2 dates and 2 amounts, simply add a second date field and a second amount field
Idea 2: If there can be an infinite number of days and amounts, you'll have to change your model to reflect it, by having :
A model for your names
A model for your days and amounts, with a foreign key to your names
Idea 3: You could keep the same model and simply remove the unique constraint, but that's a recipe for mistakes
Based on your choice, you'll then have several ways of querying what you need. It depends on your final model structure. The best way to go would be to create custom model methods that query the 2 dates/amount, format an array and return it
I have and Event and EventCategory model with a many-to-many relationship.
There are 2 event categories - past (ID of 2) and future (ID of 1).
I want to find all Events in the future via
models = Event.objects.filter(categories__slug='future')
And then update them so they are all in the past.
Should I create a list of the Event instances, then delete from the related table, and insert the instances with the new category ID - just wanted some guidance if possible, on the most efficient way to do this.
Many thanks
I am not sure if this is the most efficient solution for my needs but it works:
Find the new category I want to associate to the events (this is the "past" category)
cat = EventCategory.objects.get(pk=2)
Here are my existing relationships, where 1 is the ID of the future category
models = Event.objects.filter(categories__id=1)
for model in models:
model.categories.clear()
model.categories.add(cat)
Now all of the entires in the related table are in the past category.
Defined Models Are... Gaurd(name, category) Chapter(Gaurd, Chapter, rates) and Topic(chapter, topic_name, level) These three are connected via foreign key with each other.
I'd suggest you modify your questing and post some actual model classes along with any queries you have already tried.
Here is what the query might look like:
Topic.objects.filter(chapter__guard__name='Algebra').values_list('topic_name', flat=True)
Remove the values(...) call to get full topic objects.
I have model named IssueFlags with columns:
id, created_at, flags_id, issue_id, comments
I want to get data of unique issue_id (latest created) with info about flags_id, created_at and comments
By sql it's working like this:
SELECT created_at, flags_id, issue_id, comments
FROM Issues_issueflags
group by issue_id
How to do the same in Django? I tried to wrote sth in shell, but there is no attribute group by
IssueFlags.objects.order_by('-created_at')
This above return me only the list of ordered data.
Try doing this way:
from django.db.models import Count
IssueFlags.objects.values('created_at', 'flags_id', 'issue_id', 'comments').order_by('-created_at').annotate(total=Count('issue_id'))
I have written annotate(total=Count('issue_id')) assuming that you would have multiple entries of unique issue_id (Note that you can do all possible types of aggregations like Sum, Count, Max, Avg inside . Also, there already exists answers for, doing group by in django. Also have a look at this link or this link. Also, read this django documentation to get a clear idea on when to place values() before annotate() and when to place it after, and then implement the learning as per your requirement.
Would be happy to help if you have any further doubts.
I've been looking at the Opencart database structure and basically, they have a product table, a category table, and then a table with 2 columns which links a product_id to a category_id.
What is the reasoning behind this? Would it not make things simpler to just include the category_id as a column in the product table?
Because a product can belong to more than one category and signifies a many-to-many relationship. It is part of the normalization process.