我有以下django模型:
class OrderLog(models.Model):
order = models.ForeignKey(Order)
entry = models.TextField()
private = models.BooleanField(default=False)
及有关表格:
class OrderLogForm(forms.ModelForm):
class Meta:
model = OrderLog
fields = ('entry', 'order',
在Django中有一个模型,其中包含一个public布尔字段,该字段控制条目是公共的还是私有的。
然而,在表单中,我应该显示一个倒置逻辑:private设置的复选框。
class MyModelForm(forms.ModelForm):
private = forms.BooleanField(label="Make this entry private")
class Meta:
model = models.MyModel
从这里我该怎么走?
我想显示一个排除了某些字段的表,因为我不想在表中显示它们。同时,我希望根据排除的值更改行中的颜色。使用django-table2可以做到这一点吗?
import django_tables2 as tables
from web_logic.models import Stations
from django_tables2.utils import A
class StationTable(tables.Table):
"""
"""
station_id = tables.LinkColumn()
I Django-Rest有一个包含字段first_name的类User,以及一个包含字段username和a_class_ref (即one-to-one'关系)的类Account。
如何在B的序列化程序中执行以下操作:
class AccountSerializer():
class Meta:
model= Account
fields= [
'username',
'firstname`
]
帐户:
class Account(models.Model):
如何使场与相关模型的场相关联。我有两个模型:相册和照片,相册模型有布尔字段private。如何在照片中创建字段private,如果相册字段等于0,该字段将具有False值,如果不等于,则具有相册私有字段的值。
models.py:
from django.db import models
from django.contrib.auth.models import User
import os
import uuid
def get_image_path(instance, filename):
return '{}.{}'.format(uuid.uuid4(),
每当我定义depth属性时,POST部分中的swagger文档中的外键字段就会消失。这似乎很奇怪,因为当我想要GET请求中的相关数据时,我需要GET。因此,为了在POST部分获取相关的字段参数,我不能删除它。
情况就是这样。
模型:
from django.db import models
from django.conf import settings
# Create your models here.
User = settings.AUTH_USER_MODEL
class Todo(models.Model):
user = models.ForeignKey(User)
比如说,我有以下两个Django模型:
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
name = models.CharField(max_length=300)
pages = models.IntegerField()
author = models.ForeignKey(Author)
使用django-filter,我如何编写一个FilterSet,它允许我进行某种程度的过滤,比如:
Author.object
我正在编写django教程,并尝试使用cleaned_data属性验证表单。
forms.py文件的代码是
from django import forms
from .models import SignUp
class SignUpForm(forms.ModelForm):
class Meta:
model = SignUp
fields = ['email']
def clean_email(self):
email = self.cleaned_dat
我有以下模型
class Employee(Person):
job = model.Charfield(max_length=200)
class Address(models.Model):
street = models.CharField(max_length=200)
city = models.CharField(max_length=200)
class EmpAddress(Address):
date_occupied = models.DateField()
date_vacated = models.DateField()
employee
我简化了代码结构,其中包含两个模型:
# created by third part app, not Django one
# but we share same DB, so i have access to this one
class A(models.Model):
title = models.TextField()
# other fields ...
class Meta:
manage = False
class B(models.Model):
model_a = models.OneToOneField(A, rela
我有一个模型,其中的price是一个整数场。我确实进行了迁移,一切都很好。
from django.db import models
class Bill(models.Model):
price= models.IntegerField()
然后,由于需求变化,我不得不将price字段设置为JSONField,它将根据类似于此的某些键存储price值。
price={"actual_price":100, "tax_price":20}
我在模型中做了如下修改:
from django.db import models
class Bill(mod
请看一些片段:
1.UserProfile模型:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
email = models.EmailField(unique=True)
HEAD_CHOICE = (
('1', 'M'),
('2'
假设我有以下Django模型:
class Person(models.Model):
name = models.CharField()
age = models.IntegerField()
country = models.ForeignKey(Country)
company = models.ForeignKey(Company)
我的应用程序可以根据某些条件搜索人员,然后返回一个包含结果的表,只显示用户请求的特定列。
现在,我希望将搜索和显示的列一起存储在数据库中。
class SavedSearch(models.Model):
title
我正在努力扩展django.contrib.sites.admin。我有一个困难的时间来寻找如何添加一个字段到网站的管理页面,因为事实是,django.contrib.sites对我的外国模式一无所知。有没有一种简单的方法可以让我把下面例子中的"derp“字段添加到站点管理中?我是否必须扩展django.contrib.sites.models safe/etc功能才能做到这一点?非常感谢。
class Herp(models.Model):
site = models.ForeignKey(Site)
derp = models.CharField(blank=Tru
我有一个常规的Django User模型和一个UserDetails模型(OneToOneField和User),它是对User模型的扩展。(我尝试了Django 1.5的特性,它是一个令人头痛的奇怪可怕的文档,所以我坚持使用OneToOneField选项)
因此,在我构建一个自定义注册页面的过程中,该页面将包含由User字段和UserDetails字段组成的注册表单,因此我想知道是否有一种方法可以从这两个相关模型中自动生成表单(及其所有验证)。我知道这适用于一种由一种模型组成的表格:
class Meta:
model = MyModel
但是,对于由两个相关模型组成的一种形式,是否
我在postgresql数据库中有一个查询,这需要太多的时间,我喜欢添加一个索引来使它更快,但我不知道我应该在索引中包括哪些字段,因为它们中的许多属于其他表,其中一些是外键。
我使用hibernate,HQL查询如下:
SELECT i FROM Item i
LEFT JOIN i.model.kind AS k
LEFT JOIN i.model.kind.subkind AS s
WHERE i.file is null " +
AND i.identifier is not null
AND i.identifier != ''
AND i.place is
如果我有两个扩展抽象类的类:
public abstract class Vehicle {
private String model;
}
public class Car extends Vehicle {
private int seatingCapacity;
private double topSpeed;
}
public class Truck extends Vehicle {
private double payloadCapacity;
}
我想使用jackson (和spring-boot)在JSON中序列化数据。默认情况下,JSON如
我有很多包含相同非模型字段的表单。出于本例的目的,让我们将该字段命名为author。这不是任何模型的字段,只是我希望出现在每个表单中的字段。下面是我当前代码的一个工作示例: from django import forms
from . import models
class BlogForm(forms.Form):
author = forms.CharField()
class Meta:
model = models.Blog
fields = ["author"]
class BookForm(forms.Form
我正在内省模型中某些字段的类型,特别是我感兴趣的是检索与RDBMS相关的类型,即"VARCHAR(20)",而不是Django字段类(本例中为django.db.models.CharField)。
但是,我在关系方面有问题,因为数据库将两个表和varchar主键以及其他表与integer pks混合在一起(所以我不能做任何假设)。
到目前为止,我尝试使用以下代码检索字段类型:
# model is a django.db.model class
for field in model._meta.get_fields(include_parents=False):
tr
我正在创建模型,其中每个模型都应该与一个用户相关,如下所示:
class Item(models.Model):
user = models.ForeignKey(User)
但是在Django Admin中,对于Item,它显示了一个select字段,其中包含按用户名列出的所有用户。除了用户名之外,我还需要显示每个用户的first_name和last_name。
如何在Django Admin界面选择列表中同时显示这三个字段?