django-models

Control object creation flow in Django inheritance models

核能气质少年 提交于 2020-05-15 07:17:09
问题 I have been read some Django document about inheritance models and parent_link . Suppose that I have these models: class Parent(models.Model): #Some field goes here! class Child(Parent): #Some field goes here! I have 3 questions about this pattern: What should I do if I want to create new child object and pass id of existing parent to that? What should I do if I want to create just new child object and after a while create parent object for that child? Also I din't understand this document

Display foreign key value in django template

老子叫甜甜 提交于 2020-05-13 18:53:47
问题 I've looked through the similar questions and was unable to find a solution that fits or I'm missing something? I have two models(SafetyCourse and SafetyCourseTaken) I have a foreign key relationship that points from "safety courses taken" to safety course, shown below: models.py class SafetyCourse(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) name = models.CharField(max_length=128, unique=True) def __str__(self): return self.name class

Display foreign key value in django template

老子叫甜甜 提交于 2020-05-13 18:52:37
问题 I've looked through the similar questions and was unable to find a solution that fits or I'm missing something? I have two models(SafetyCourse and SafetyCourseTaken) I have a foreign key relationship that points from "safety courses taken" to safety course, shown below: models.py class SafetyCourse(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) name = models.CharField(max_length=128, unique=True) def __str__(self): return self.name class

Display foreign key value in django template

て烟熏妆下的殇ゞ 提交于 2020-05-13 18:52:25
问题 I've looked through the similar questions and was unable to find a solution that fits or I'm missing something? I have two models(SafetyCourse and SafetyCourseTaken) I have a foreign key relationship that points from "safety courses taken" to safety course, shown below: models.py class SafetyCourse(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) name = models.CharField(max_length=128, unique=True) def __str__(self): return self.name class

Django 1.8 OperationalError: no such column:

自闭症网瘾萝莉.ら 提交于 2020-05-13 05:42:12
问题 I'm using django 1.8 and I'm having problems adding to my models.py. Currently it's: from django.db import models # Create your models here. class Company(models.Model): role = models.CharField(max_length=32, blank=True) name = models.CharField(max_length=70, blank=True) and it works perfectly fine but whenever I try to add to this and then run the server I get OperationalError: no such column [added element] For example I added founder = models.CharField(max_length=200, blank=True) and I ran

How to get only latest record on filter of foreign key django

南笙酒味 提交于 2020-05-12 04:49:09
问题 I have a table like this Event table | id | status | date |order(FK)| | 1 | Planned | 05-02-2015 | 1 | | 2 | Delivered | 04-02-2015 | 2 | | 3 | Packed | 03-02-2015 | 3 | | 4 | Return | 06-02-2015 | 1 | I want output like this | id | status | date |order(FK)| | 2 | Delivered | 04-02-2015 | 2 | | 3 | Packed | 03-02-2015 | 3 | | 4 | Return | 06-02-2015 | 1 | I tried with query = Event.objects.annotate(order_num=Max('date')) but didn't got the expected result. How can i achieve this output 回答1:

Python type hinting: how to tell X is a subclass for Foo?

浪子不回头ぞ 提交于 2020-05-11 03:43:25
问题 How should I write a type hint for class types in Python? Consider this code: class A(object): pass class B(A): pass def register(cls: type[A]): assert issubclass(cls, A) register(A) register(B) Is type[A] the correct way to write this? If I'd just use cls: A it would mean cls is an instance of A , but I want to to say cls is a class/type, which at least subclasses A . Specifically, what I want to indicate is that the parameter should be a Django model type. 回答1: It seems like other current

Python type hinting: how to tell X is a subclass for Foo?

牧云@^-^@ 提交于 2020-05-11 03:43:09
问题 How should I write a type hint for class types in Python? Consider this code: class A(object): pass class B(A): pass def register(cls: type[A]): assert issubclass(cls, A) register(A) register(B) Is type[A] the correct way to write this? If I'd just use cls: A it would mean cls is an instance of A , but I want to to say cls is a class/type, which at least subclasses A . Specifically, what I want to indicate is that the parameter should be a Django model type. 回答1: It seems like other current

Django reset auto-increment pk/id field for production

浪子不回头ぞ 提交于 2020-05-10 14:11:10
问题 (I'm new to Django, Python, and Postgresql) I've been adding and deleting data in my development and noticed that the pk keeps adding up and never reset to 1 even if I delete all the models. Is it possible to reset the pk to start from 1 before I push this up to the production? Is it a good idea to do that? 回答1: You can reset model id sequence using sqlsequencereset command python manage.py sqlsequencereset myapp1 myapp2 myapp3| psql If you want to read the generated sql command, just execute

(Django) ORM in airflow - is it possible?

时光怂恿深爱的人放手 提交于 2020-05-10 07:42:23
问题 How to work with Django models inside Airflow tasks? According to official Airflow documentation, Airflow provides hooks for interaction with databases (like MySqlHook / PostgresHook / etc) that can be later used in Operators for row query execution. Attaching the core code fragments: Copy from https://airflow.apache.org/_modules/mysql_hook.html class MySqlHook(DbApiHook): conn_name_attr = 'mysql_conn_id' default_conn_name = 'mysql_default' supports_autocommit = True def get_conn(self): """