django-orm

Django - save changes made to model objects and render when published

谁都会走 提交于 2019-12-24 19:25:57
问题 I am new to Django and working on my first project in Django. I am providing a simpler example for my application where - I have 3 different types of users for the application. Teacher's Assistant - Schedules/plans the timetable and courses for the teacher depending on day's agenda Teacher - approves the items on the agenda and marks if an item on the schedule is completed Student - views the schedule of an individual teacher Currently, I have this whole application ready with all its

filter company based on category

六眼飞鱼酱① 提交于 2019-12-24 18:57:40
问题 I was doing the project on Django to get into it more deeply. I have a problem in the model part. There are models; Company , Product , Category . Company is simply the introduction part. Product is about what product a company has and its divided into category which is ManyToManyField . I have list all the categories in a page where if certain category is clicked then the list of companies that has product of that category should be filtered. But I don't know how to access it. Here is my

What minimal files i need to use django ORM

会有一股神秘感。 提交于 2019-12-24 18:53:27
问题 I have python module for doing some stuff and i need to save few items in database. Currenlt i am using raw sql to insert data. But i want to use django ORM for that. I don't need any urls , views etc. all i want is that i can create models and then should be able to save it like user.save() Now i don't want to have unnecessary files and data in there . but i am not sure which files i need to have. Do i need settings.py urls.py views.py app folder. Is it possible to have just models.py and

Django manytomany restriction

守給你的承諾、 提交于 2019-12-24 17:37:25
问题 I am new in database and Django. I have very little knowledge on trigers. I created a database about an academic expert system and here all the papers should have up to 4 topics. If more topics are selected than ther should be a warning as "too many topics". A topic could be owned by many papers. So it is kind of many-to-4 relation. But I do not know how to limit the upper bound to 4. My classes are like this: class Topic(models.Model): name = models.CharField(max_length=200) title = models

Django ORM Annotate boolean field that defines is user member of a team

随声附和 提交于 2019-12-24 15:08:08
问题 models.py from django.db import models class UserGroup(models.Model): members = models.ManyToManyField(User, related_name='members', through='UserGroupMember') class UserGroupMember(models.Model): user = models.ForeignKey(User) usergroup = models.ForeignKey(UserGroup) class Cohort(models.Model): user_groups = models.ManyToManyField(UserGroup) class Team(models.Model): cohort = models.ForeignKey(Cohort) members = models.ManyToManyField(User, related_name='team_members', through='TeamMembers',

How to use the ORM for the equivalent of a SQL count, group and join query?

别等时光非礼了梦想. 提交于 2019-12-24 07:51:35
问题 I have tags which can be associated with images and locations. Tags are unique. Images and locations can have many tags. A Tag_Item model is used to link everything together. Here are the models: LOCATIONS = ( ('US', 'USA'), ('UK', 'United Kingdom'), ('FR', 'France'), ) class Location(models.Model): location = models.CharField(choices=LOCATIONS) class Image(models.Model): image = models.ImageField(verbose_name='Image') class Tag(models.Model): tag = models.CharField(max_length=150, unique

Django ORM Left Join With GROUP BY and SUM

我的未来我决定 提交于 2019-12-24 04:16:17
问题 I am using Django 1.8. I have a User model and a UserAction model. A user has a type. UserAction has a time, which indicates how long the action took. They look like this: class User(models.Model): user_type = models.IntegerField() class UserAction: user = models.ForeignKey(User) time = models.IntegerField() Now what I want to do is get ALL users of a certain type along with the sum of the time of their actions.So it would look something like this: {user:1,total_time=5000}, {user:2,total_time

Django - Multiple Databases

▼魔方 西西 提交于 2019-12-24 00:45:53
问题 So I'm somewhat new to the whole Django databases and maybe I just don't fully understand the Django routers talked about here: https://docs.djangoproject.com/en/dev/topics/db/multi-db/#database-routers but for the life of me I cant figure out how to link two databases together. Maybe it's because my set-up is different? The two databases are separate Django project folders and both have separate Postgre databases. I think that maybe the problem comes form them being in different folders and

Django Models Group By

故事扮演 提交于 2019-12-23 23:19:24
问题 I have this simple SQL query - SELECT pid, COUNT(*) AS docs FROM xml_table WHERE suid='2' GROUP BY pid; How do I get this using Django ORM (i.e. django models). Basically I am not getting how to do GROUP BY ? 回答1: XML_table.objects.filter(suid='2').values('pid').annotate(docs=Count('pid')).order_by() Docs 回答2: This works very nicely. from collections import defaultdict count = defaultdict( int ) for doc in XML_Table.objects.filter(suid='2'): count[doc.pid] += 1 It's not SQL. Often it's faster

Is there a way to make a query respect the order of the inputted parameters?

旧街凉风 提交于 2019-12-23 19:41:49
问题 (Please let me know if this is completely absurd, that is probably a reason why I haven't found anything on this.) This story has two models Ranking and Artist , Ranking is generically related to Artist (object_id, content_type... the whole shebang). I have a list of objects returned by Ranking.objects.values_list() ordered by a certain field (in my case score ). So obviously, if I want to display a list of artists that were ranked, I'd want them in the same order. I've tried different