我目前正在使用Django创建一个社交平台。目前,我正在开发主页,并希望显示匹配用户字段的帖子。这是我的帖子模型: class Discussion(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Account, on_delete=models.CASCADE)
post = models.TextField()
date = models.DateTimeField(default=timezone.now) 这是用户帐户模型: clas
我有一个django项目,我有一个Post模型,它看起来像这样:
class BasicPost(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
published = models.BooleanField(default=False)
created_date = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, bla
我最近一直在学习Django,但我有一个复杂的情况,我不确定正确的子查询应该是什么。
用户可以在属于主题的帖子上发表评论。对于特定的用户,我想找到他们评论最多的主题。平局可以任意打破。
以下是模型的相关部分:
class Topic(models.Model):
name = models.CharField(max_length=10)
title = models.CharField(max_length=10)
class Post(models.Model):
topic - models.ForeignKey(Topic, on_delete=models.
在这里创建你的模型。
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
STATUS_CHOICES = (('draft', 'Draft'), ('published', 'Published'),)
title = models.CharField(max_length=250)
sl
我正在使用django开发一个商业网站。登录用户具有创建列表的能力。我使用django模型为表单创建模型和ModelForm。
models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
pass
class Listing(models.Model):
title = models.CharField(max_length=100)
description = models.TextFiel
我已经为这个问题挣扎了好几天了,其他关于这个问题的帖子似乎帮不了我。因此,我已经将用户模型与我自己的Gebruiker模型连接起来,该模型有城市、邮政编码等字段。现在,我设法将它们显示在寄存器表上,但它们似乎不起作用。我的第二个问题是关于输入字段的顺序,每次它们以随机的顺序出现。我怎样才能建立稳定的秩序?这是我的forms.py
from django import forms
from django.contrib.auth.models import User
from .models import Gebruiker
from django.contrib.auth.forms impo
我扩展了Django用户模型,并在其中添加了一些字段。然后,我保存了模型,并使用StackedInline替换了最后一个用户模型。为此目的,我使用django_rest_framework。更具体地说,这里是我的代码:
Models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# TODO: add Upload directory to the avatar
avatar = models.ImageField()
我正在构建一个与游戏成就有关的应用程序,我想为每个游戏显示一个页面,列出相关的成就。这就完成了。
然而,我也希望每个用户能够在列表中看到他们已经完成了哪些成就。
我的模型是这样的:
class Achievement(models.Model):
game = models.ForeignKey(Game)
...
class Game(models.Model):
...
class GameProfile(models.Model):
achievements = models.ManyToManyField(Achievement, through=&
我有用户模型和地址,SocialProfile模型。我想在active admin中用户资源的filter部分显示地址模型的字段,如"zip“、"address_line_1”。
作为地址模型,我希望在相同的用户资源中显示SocialProfile模型的字段。如何在belongs_to模型的主动管理中显示下拉和文本搜索中的字段?
class User < ActiveRecord::Base
has_many :addresses
has_many :social_profiles
end
class Address < ActiveRecord::Bas
我需要建立一个web表单与选择字段的数量,这些字段的选择取决于对以前的字段的答案。一个典型的例子是vehicle year > make > model表单,其中make的选择选项只有在选择了某个年份之后才会填充,而model的选择选项只有在选择make之后才会填充。填充每个单独字段的sql将如下所示:
select distinct year from cars
select distinct make from cars where year = [answer above]
select distinct model from cars where year = [ans
我在Django项目中有一个Post模型。当用户收到帖子列表时,当他收到post实例时,我想显示description字段,而不是显示body字段。(模型中这两个字段分别存在)
这是我的邮政序列化程序:
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ['id', 'title', 'image', 'description', 'slug']
我目前正在django中构建一个页面,其中有4个表单域,2个文本,2个选择域,当提交时,它接受这些域并在几个模型中搜索匹配的项。
该模型如下所示:
class Person(models.Model):
user = models.ForeignKey(User, blank=True, null=True, verbose_name="the user associated with this profile")
first_name = models.CharField(max_length=255)
last_name = models.CharF
我在这里特别询问Django的情况,但我希望有任何见解。
我现在正在写一个基本的社交网络。我的计划是允许用户发布不同类型的帖子(即文本帖子、图像帖子和视频帖子),然后允许他们在自己的提要上按时间顺序访问他人的帖子。提要需要包含所有这些不同类型的帖子。
我认为这样做很简单,方法是将一般的Post类子类为TextPost类、ImagePost类等等,然后简单地在提要上显示每个帖子。
#What I thought I would do
class Post(models.Model):
user = models.ForeignKey(User, editable = False)
我试图在django的postgres db模型中添加标记,我找到了两个解决方案:
使用外键:
class Post(models.Model):
tags = models.ManyToManyField('tags')
...
class Tag(models.Model):
name = models.CharField(max_length=140)
使用数组字段:
from django.contrib.postgres.fields import ArrayField
class Post(models.Model):
tags