django-models

Django nested QuerySets

ぐ巨炮叔叔 提交于 2021-02-18 07:32:00
问题 I have a Django data model like this (data fields omitted): class Atom(Model): pass class State(Model): atom = ForeignKey(Atom) class Transition(Model): atom = ForeignKey(Atom) upstate = ForeignKey(State,related_name='uptrans') lostate = ForeignKey(State,related_name='lotrans') When I query, the fields to be restricted can be in either model, so it is easiest to query on Transition.objects.filter(...) since all fields in the other models can be reached through the foreign keys. Let's call the

Django nested QuerySets

回眸只為那壹抹淺笑 提交于 2021-02-18 07:31:16
问题 I have a Django data model like this (data fields omitted): class Atom(Model): pass class State(Model): atom = ForeignKey(Atom) class Transition(Model): atom = ForeignKey(Atom) upstate = ForeignKey(State,related_name='uptrans') lostate = ForeignKey(State,related_name='lotrans') When I query, the fields to be restricted can be in either model, so it is easiest to query on Transition.objects.filter(...) since all fields in the other models can be reached through the foreign keys. Let's call the

What is doing __str__ function in Django?

爷,独闯天下 提交于 2021-02-18 05:05:03
问题 I'm reading and trying to understand django documentation so I have a logical question. There is my models.py file from django.db import models # Create your models here. class Blog(models.Model): name = models.CharField(max_length=255) tagline = models.TextField() def __str__(self): return self.name class Author(models.Model): name = models.CharField(max_length=255) email = models.EmailField() def __str__(self): return self.name class Post(models.Model): blog = models.ForeignKey(Blog)

What is doing __str__ function in Django?

烈酒焚心 提交于 2021-02-18 05:01:06
问题 I'm reading and trying to understand django documentation so I have a logical question. There is my models.py file from django.db import models # Create your models here. class Blog(models.Model): name = models.CharField(max_length=255) tagline = models.TextField() def __str__(self): return self.name class Author(models.Model): name = models.CharField(max_length=255) email = models.EmailField() def __str__(self): return self.name class Post(models.Model): blog = models.ForeignKey(Blog)

django days-of-week representation in model

流过昼夜 提交于 2021-02-17 09:55:57
问题 I have this "Jobs Server" model that i'm building. I want to include a field that will save which days of the week this job will run on. Ultimately in the UI, i would like the user to be able to have a series of check boxes(one for each day) that they can select. What would be the best way to represent this "days-of-week" data in my mode? class Job(models.Model): name = models.CharField(max_length=32, unique=True) package = models.ForeignKey(Package) binary = models.ForeignKey(Binary) host =

ModuleNotFoundError and ImportError even beeing right

喜你入骨 提交于 2021-02-17 07:10:45
问题 Hello, thanks for your time. i'm trying to import models on seeders.py. Can someone please tell me what am i doing wrong, i've done this a hundred times and tried every single way: from winners.models import Player or from ..models import Player models.py: from django.db import models class Player (models.Model): name = models.CharField(max_length=100) sex = models.CharField(max_length=9, choices=sex_choices) age = models.PositiveIntegerField() height = models.DecimalField(max_digits=3,

ModuleNotFoundError and ImportError even beeing right

六眼飞鱼酱① 提交于 2021-02-17 07:09:41
问题 Hello, thanks for your time. i'm trying to import models on seeders.py. Can someone please tell me what am i doing wrong, i've done this a hundred times and tried every single way: from winners.models import Player or from ..models import Player models.py: from django.db import models class Player (models.Model): name = models.CharField(max_length=100) sex = models.CharField(max_length=9, choices=sex_choices) age = models.PositiveIntegerField() height = models.DecimalField(max_digits=3,

function UNIX_TIMESTAMP does not exist

断了今生、忘了曾经 提交于 2021-02-17 06:12:14
问题 I am trying to convert my datetime to unix timestamp. I've tried doing several ways and the error is all the same. I am not very sure what I am suppose to do. date_joined is like this 2017-09-30 10:24:44.954981+00:00 function unix_timestamp(timestamp with time zone) does not exist HINT: No function matches the given name and argument types. You might need to add explicit type casts. User.objects.annotate(photo_time=Func(F('date_joined'),function='UNIX_TIMESTAMP')) User.objects.extra(select={

Django don't generate primary_key

故事扮演 提交于 2021-02-17 05:33:09
问题 I write the migration manually to quickly add to the new environment. When I try to create a new object of the Operator model I get an error about an empty id. I tried to set managed = False in meta and fake Operator model but none of this brought results. What's wrong with my code? My model: class Operator(models.Model): id = models.IntegerField(primary_key=True) name = models.CharField(max_length=256) class Meta: db_table = '"mts_market"."operator"' managed = False def __str__(self): return

Django FileField default file

自古美人都是妖i 提交于 2021-02-16 05:12:20
问题 I have a model which contains FileField as below class Employer(models.Model): logo = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='logos') The question is how can I add a default file like "{{ MEDIA_ROOT}}/logos/anonymous.jpg" to this filefield ? 回答1: You can specify the default file to use for that field as follows: class Employer(models.Model): logo = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='logos', default=