我正在制作的程序是显示数据库中的产品信息。 程序运行良好,问题是我想按姓氏而不是“所有者”订购。例如如果客户用户名是ewOI,这是“所有者”,他的名字是Robert,姓是Bosh,我想按姓而不是所有者订购queryset
以下是视图代码:
class ExpView(ListView):
context_object_name = 'product'
queryset = Product.objects.filter(prod__isnull=True).order_by('owner')
template_name = 'productrecords.html'
paginate_by = 10
有两个模型,一个在产品中,一个在用户中 产品型号:
class Product(models.Model):
owner = models.ForeignKey(
User, on_delete=models.PROTECT, related_name='products'
)
id_subscription = models.CharField(max_length=30, unique=True)
amount = models.DecimalField(max_digits=11, decimal_places=2, null=True)
interest_rate = models.DecimalField(max_digits=4, decimal_places=2)
start_date = models.DateField(null=True)
以及用户模型:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.PROTECT, primary_key=True)
second_name = models.CharField(max_length=32, blank=True)
last_name2 = models.CharField(max_length=32, blank=True)
recovery_email = models.EmailField(unique=True, null=True)
phone_number = models.CharField(max_length=16, unique=True, null=True)
但我想按(‘姓’)点菜,但那不行。我怎么点
多谢各位
你试过这个吗
您可以通过键入\uu从外键调用字段
在按参数指定顺序时,也可以引用相关模型。像这样:
它应该会起作用。 (尽管如此,如果用户模型有last_name字段,我想知道在概要文件中使用last_name 2的原因是什么。)
相关问题 更多 >
编程相关推荐