django-models

How to left outer join with extra condition in Django

北战南征 提交于 2020-03-17 07:04:06
问题 I have these three models: class Track(models.Model): title = models.TextField() artist = models.TextField() class Tag(models.Model): name = models.CharField(max_length=50) class TrackHasTag(models.Model): track = models.ForeignKey('Track', on_delete=models.CASCADE) tag = models.ForeignKey('Tag', on_delete=models.PROTECT) And I want to retrieve all Tracks that are not tagged with a specific tag. This gets me what I want: Track.objects.exclude(trackhastag__tag_id='1').only('id') but it's very

How to left outer join with extra condition in Django

岁酱吖の 提交于 2020-03-17 07:04:02
问题 I have these three models: class Track(models.Model): title = models.TextField() artist = models.TextField() class Tag(models.Model): name = models.CharField(max_length=50) class TrackHasTag(models.Model): track = models.ForeignKey('Track', on_delete=models.CASCADE) tag = models.ForeignKey('Tag', on_delete=models.PROTECT) And I want to retrieve all Tracks that are not tagged with a specific tag. This gets me what I want: Track.objects.exclude(trackhastag__tag_id='1').only('id') but it's very

django models: get list of id

拥有回忆 提交于 2020-03-17 04:06:29
问题 How do i get a list of all id/primary key for a table. Say i have this table: class Blog(models.Model) title = models.CharField() body = models.CharField() author = models.ForeignKey(Author) assume the field author is an Author object. I want to get all the ids of Blog where author=author i know i can use blogs = Blog.objects.filter(author=author) and get all the blog objects in a list form, but how do i get the list IDS/PK? Similar to "Select id from Blog where Author=author" 回答1: You can do

How to modify a models who's already migrated in Database?

你。 提交于 2020-03-16 07:16:09
问题 I am new to django, I want to modify my models (Which is already in my sqlite3 database) without bugging my entire project. Last time I modified my models it took me hours to fix it cause I bugged the whole project. So I dont want to make the same error, could you guys help me witht the commands please? Thanks for helping models.py (at the moment) from django.db import models THE_GENDER = [ ("Monsieur", "Monsieur"), ("Madame", "Madame") ] class Post(models.Model): name = models.CharField(max

Apply migrations and models from all the apps

好久不见. 提交于 2020-03-16 05:20:04
问题 I'm using Django and I have an schema like mainapp |---mainapp | |---migrations.py | |---models/ |---app2 |---migrations/ |---models/ But, when I execute: python manage.py migrate it is generationg the tables of mainapp/models , but no the app2/models and app2/migrations either. How can execute those migrations? 回答1: first of all try python manage.py makemigrations for a specific app python manage.py makemigrations appname this will migrate all the apps then python manage.py migrate hope it

Get relationship model name of object Django

旧巷老猫 提交于 2020-03-05 01:43:07
问题 I'm trying to get the model name for different objects: This kinda works: last_profile = Profile.object.last() profile.__class__._meta.verbose_name // 'profile' But not for a compounded model name such ProfileLogin : last_profile_login = ProfileLogin.objects.last() last_profile_login.__class__._meta.verbose_name // 'profile login' I would like the last example to be profile_login . The reason, I'm doing this is to create a dynamic query using filter(). 来源: https://stackoverflow.com/questions

How to use validators in django

隐身守侯 提交于 2020-03-03 10:01:08
问题 from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ def validate_subject(value): if value.isalnum(): raise ValidationError(_('%(value)s is not valid. Please use alphanumneric characters as subject names'), params={'value': value},) class Exam(models.Model): #Exam can have many questions subject = models.TextField(primary_key=True, unique = True, validators = [validate_subject]) #make it to reject a string of length 0 def __str__(self):

Django Manager isn't available; 'auth.User' has been swapped for 'users.MyUser'

孤人 提交于 2020-03-03 03:06:48
问题 I created Custom UserModel in Django 1.11 and I need a function which allows users to sign in I think my custom user model is incompatible with my function How can I fix? see error message in the image below : users.models.py from django.db import models from django.contrib.auth.models import AbstractBaseUser, BaseUserManager from apps.teams.models import Team import uuid class MyUserManager(BaseUserManager): def _create_user(self, username, password, **extra_kwargs): user = self.model

Django Manager isn't available; 'auth.User' has been swapped for 'users.MyUser'

喜夏-厌秋 提交于 2020-03-03 03:04:50
问题 I created Custom UserModel in Django 1.11 and I need a function which allows users to sign in I think my custom user model is incompatible with my function How can I fix? see error message in the image below : users.models.py from django.db import models from django.contrib.auth.models import AbstractBaseUser, BaseUserManager from apps.teams.models import Team import uuid class MyUserManager(BaseUserManager): def _create_user(self, username, password, **extra_kwargs): user = self.model

Django chaining prefetch_related and select_related

假装没事ソ 提交于 2020-03-02 19:32:26
问题 Let's say I have following models class Foo(models.Model): ... class Prop(models.Model): ... class Bar(models.Model): foo: models.ForeignKey(Foo, related_name='bars', ...) prop: models.ForeignKey(Prop, ...) Now I want to make the following query. foos = Foo.objects.prefetch_related('bars__prop').all() Does the above query makes 3 Database calls or only 2 (select_related for prop from bar ), given that only one prop is associated with bar If it takes 3 calls then, is there a way to make it 2