django-orm

Migrations in stand alone Django app

半城伤御伤魂 提交于 2019-12-01 06:40:06
How do I makemigrations on a stand alone Django app (ie one that is not part if any project). For example after following: https://docs.djangoproject.com/en/1.8/intro/reusable-apps/ You can do it similar to how you do testing scripts for apps: #!/usr/bin/env python import sys import django from django.conf import settings from django.core.management import call_command settings.configure(DEBUG=True, INSTALLED_APPS=( 'django.contrib.contenttypes', 'your_app', ), ) django.setup() call_command('makemigrations', 'your_app') What I do is to create a mock project, containing only that app, then the

How to enforce char(N) datatype instead of varchar(N) in django model field

牧云@^-^@ 提交于 2019-12-01 06:13:45
问题 As per django docs https://docs.djangoproject.com/en/1.9/topics/db/models/ it's ORM create varchar field instead of char . from django.db import models class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) and equivalent sql statement CREATE TABLE myapp_person ( "id" serial NOT NULL PRIMARY KEY, "first_name" varchar(30) NOT NULL, "last_name" varchar(30) NOT NULL ); so here we can see it is using varchar, but can I use char instead

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

只愿长相守 提交于 2019-12-01 05:12:56
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.filter(??) If you use PostgreSQL or another DB backend with support for DISTINCT ON there is a nice

How to recursively query in django efficiently?

橙三吉。 提交于 2019-12-01 05:11:10
I have a model, which looks like: class StaffMember(models.Model): id = models.OneToOneField(to=User, unique=True, primary_key=True, related_name='staff_member') supervisor = models.ForeignKey(to='self', null=True, blank=True, related_name='team_members') My current hierarchy of team is designed in such a way that there is let's say an Admin (who is at the top most point of hierarchy). Now, let's say 3 people (A, B, C) report to Admin and each one of A, B and C have their own team reporting to them and so on. I want to find all the team members (boiling down to the bottom most level of

AttributeError: 'ManyToManyField' object has no attribute '_m2m_reverse_name_cache'

爷,独闯天下 提交于 2019-12-01 02:18:09
问题 I'm trying to assign Project Manager to Employee. Every Employee, can be Project Manager. Project Managers can have multiple Employees. Employees can have only 1 Project Manager. But When I do Employee.objects.get(name='HereHere').get_xxx() I got AttributeError: 'ManyToManyField' object has no attribute '_m2m_reverse_name_cache' class Employee(models.Model): name = models.CharField(max_length=20, unique=True) pm = models.ManyToManyField('self', symmetrical=False, through='PM', related_name=

Django ORM - Grouped aggregates with different select clauses

被刻印的时光 ゝ 提交于 2019-11-30 23:45:38
Imagine we have the Django ORM model Meetup with the following definition: class Meetup(models.Model): language = models.CharField() speaker = models.CharField() date = models.DateField(auto_now=True) I'd like to use a single query to fetch the language, speaker and date for the latest event for each language. >>> Meetup.objects.create(language='python', speaker='mike') <Meetup: Meetup object> >>> Meetup.objects.create(language='python', speaker='ryan') <Meetup: Meetup object> >>> Meetup.objects.create(language='node', speaker='noah') <Meetup: Meetup object> >>> Meetup.objects.create(language=

Postgres: values query on json key with django

拜拜、爱过 提交于 2019-11-30 20:18:13
I need to do a values/values_list query on nested key on a postgres backed jsonfield in django 1.10 eg. class AbcModel(models.model): context = fields.JSONField() If it has values like: { 'lev1': { 'lev': 2 } } I want to run a queries like AbcModel.objects.values('context__lev1__lev2').distinct() AbcModel.objects.values_list('context__lev1__lev2', flat=True).distinct() EDIT: The JSON fields are the official django JSONField from django.contrib.postgres.fields So I found a solution, this works with django 1.10 and above. I used the KeyTransform to annotate and extract the nexted key and did a

How to filter django model by its objects in many-to-many field (exact match)?

我是研究僧i 提交于 2019-11-30 18:09:34
I have this model in my code: class Conversation(models.Model): participants = models.ManyToManyField(User, related_name="message_participants") and I need to filter this "Conversation" model objects by the "participants" many-to-many field. meaning: I have for example 3 User objects, so I want to retrieve the only "Conversation" objects that has this 3 Users in it's "participants" field. I tried doing this: def get_exist_conv_or_none(sender,recipients): conv = Conversation.objects.filter(participants=sender) for rec in recipients: conv = conv.filter(participants=rec) where sender is a User

Django ORM - Grouped aggregates with different select clauses

会有一股神秘感。 提交于 2019-11-30 17:50:11
问题 Imagine we have the Django ORM model Meetup with the following definition: class Meetup(models.Model): language = models.CharField() speaker = models.CharField() date = models.DateField(auto_now=True) I'd like to use a single query to fetch the language, speaker and date for the latest event for each language. >>> Meetup.objects.create(language='python', speaker='mike') <Meetup: Meetup object> >>> Meetup.objects.create(language='python', speaker='ryan') <Meetup: Meetup object> >>> Meetup

Django ORM - mock values().filter() chain

我是研究僧i 提交于 2019-11-30 17:39:43
I am trying to mock a chained call on the Djangos model.Manager() class. For now I want to mock the values() and filter() method. To test that I created a little test project: Create a virtual environment Run pip install django mock mock-django nose django-nose Create a project django-admin.py startproject mocktest Create an app manage.py startapp mockme Add django_nose and mocktest.mockme to INSTALLED_APPS (settings.py) Add TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' to settings.py To verfiy that everything is setup correctly I ran manage.py test . One test is run, the standard test