django-orm

Django Admin inline for recursive ManyToMany

五迷三道 提交于 2020-01-01 04:22:09
问题 I have the following model with a many-to-many relationship to itself class Ticket(models.Model): STATUS = ( (0, "Open"), (1, "Closed"), ) status = models.SmallIntegerField(default=0,choices=STATUS) title = models.CharField(max_length=100) replies = models.ManyToManyField('self') description = models.TextField() i am trying to display this model as an inline in the admin, using the following code class TicketReply(admin.TabularInline): model = Ticket.replies.through however i keep getting

django - Get the set of objects from Many To One relationship

ぐ巨炮叔叔 提交于 2020-01-01 02:07:50
问题 Please have a look at these models: class Album(models.Model): user = models.ForeignKey(User) name = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) class Photo(models.Model): album = models.ForeignKey(Album, default=3) image = models.ImageField(upload_to=get_upload_file_name) caption = models.CharField(max_length=200) pub_date = models.DateTimeField(default=datetime.now) How do I get the the set of photos for a particular album??? And how to get the

Django Union Query

瘦欲@ 提交于 2019-12-31 04:21:10
问题 I need to develop a UNION query in Django with 3 models namely WebQuery,WebReply and BusinessOwners and the output should be of the form below. { "(#conversation_id#)_(#b_id#)": { "from": "(#user_id)", "email": "(#user_email)", "date_time": "#get from db", "query": "are you open ?", "from_r_id": "(#representative_id)", "from_r_name": "(#rep_name)", "business_registered": "FALSE" "to_business_name": "CCD saket", "chat": [{ "direction": 1, "text": "yes sir", "date_time": "424 577" }, {

Django custom user field clashes with AbstractBaseUser

守給你的承諾、 提交于 2019-12-30 11:41:47
问题 I am building a Django project from an existing database. The database is being used by other systems, so I cannot change its schema. This is my current custom User model: class Users(AbstractBaseUser): id_user = models.IntegerField(primary_key=True) role = models.IntegerField() username = models.CharField(max_length=50, unique=True) last_login_date = models.DateTimeField() AbstractBaseUser needs a column named last_login , while current database table has last_login_date column which serves

Django queryset to return first of each item in foreign key based on date

安稳与你 提交于 2019-12-30 09:23:30
问题 need to get a queryset with the first book (by a date field) for each author (related to by foreign key) ...is there a Django ORM way to do this (without custom SQL preferred but acceptable) *Edit: Please note that an answer that works using only a modern open source backend like Postgresql is acceptable ..still ORM based solution preferred over pure custom sql query) Models class Book(Model): date = Datefield() author = ForeignKey(Author) class Author(Model): name = CharField() Book.objects

Django queryset to return first of each item in foreign key based on date

浪尽此生 提交于 2019-12-30 09:23:26
问题 need to get a queryset with the first book (by a date field) for each author (related to by foreign key) ...is there a Django ORM way to do this (without custom SQL preferred but acceptable) *Edit: Please note that an answer that works using only a modern open source backend like Postgresql is acceptable ..still ORM based solution preferred over pure custom sql query) Models class Book(Model): date = Datefield() author = ForeignKey(Author) class Author(Model): name = CharField() Book.objects

Django queryset to return first of each item in foreign key based on date

不问归期 提交于 2019-12-30 09:22:08
问题 need to get a queryset with the first book (by a date field) for each author (related to by foreign key) ...is there a Django ORM way to do this (without custom SQL preferred but acceptable) *Edit: Please note that an answer that works using only a modern open source backend like Postgresql is acceptable ..still ORM based solution preferred over pure custom sql query) Models class Book(Model): date = Datefield() author = ForeignKey(Author) class Author(Model): name = CharField() Book.objects

Updating selection of objects, each with a different value in bulk (Django)

核能气质少年 提交于 2019-12-30 04:20:27
问题 Imagine I have a python dictionary where keys are existing user ids, and values are scores to be added to those users' existing scores. For example: {1: 1580, 4: 540, 2: 678} (this could stretch to n k,v pairs) I need to update the scores of all these user objects (updated_score = original_score + new_score). One way to do it is iteratively, like so: from django.db.models import F scores = {1: 1580, 4: 540, 2: 678} for user_id,score_to_add in scores.iteritems(): UserProfile.objects.filter

Django 1.8 ArrayField append & extend

[亡魂溺海] 提交于 2019-12-30 03:51:04
问题 Django 1.8 will come with new advanced field types including ArrayField these rely on PostgreSQL and are implemented at a DB level. PostgreSQL's array field implements an append method. However I can't find any documentation on appending an item to an ArrayField . This would clearly be very useful as it would allow the field to be updated without transferring its entire contents from the db then back to it. Is this possible? If not will it be possible in future? Any pointers to docs I've

Django equivalent of SQL not in

限于喜欢 提交于 2019-12-29 04:28:09
问题 I have a very simple query: select * from tbl1 where title not in('asdasd', 'asdasd') . How do I translate that to Django? It's like I want the opposite of: Table.objects.filter(title__in=myListOfTitles) 回答1: try using exclude Table.objects.exclude(title__in=myListOfTitles) 回答2: Table.objects.exclude(title__in=myListOfTitles) 来源: https://stackoverflow.com/questions/9003518/django-equivalent-of-sql-not-in