我想在管理中的多对多关系上使用 raw_id_fields,并且希望每个相关对象显示在自己的行上(而不是单个字段中的逗号分隔列表,这是默认行为)。根据在野外发现的示例,我似乎应该能够做到这一点:
# models.py
class Profile(models.Model):
...
follows = models.ManyToManyField(User,related_name='followees')
# admin.py
class FollowersInline(admin.TabularInline):
model = Profile
raw_id_fields = ('follows',)
extra = 1
class ProfileAdmin(admin.ModelAdmin):
search_fields = ('user__first_name','user__last_name','user__username',)
inlines = (FollowersInline,)
admin.site.register(Profile,ProfileAdmin)
Run Code Online (Sandbox Code Playgroud)
但这会产生错误:
<class 'bucket.models.Profile'> has no ForeignKey to <class 'bucket.models.Profile'>
Run Code Online (Sandbox Code Playgroud)
我不清楚我在这里做错了什么。感谢您的建议。
我有 2 张表“部门”和“员工”
Dept has 2 columns ID and Name
Employees has 3 columns ID, Dept_ID, Name
Run Code Online (Sandbox Code Playgroud)
现在给定员工姓名“XYZ”,我必须检索员工姓名为“XYZ”的所有部门名称
我知道他们的解决方法是首先having name == "XYZ"从 Employee 表中检索所有 dept_id,然后进行另一个查询以从 Dept 表中检索部门名称。
他们是在单个查询中检索记录的一种方法吗?
我正在尝试在 Django 中编写一个自定义 PostgreSQL 函数,它将强制日期时间为查询集中指定的时区。我对 db 函数的第一次调用如下所示:
from django.db.models.expressions import Func
class DateTimeInTimezone(Func):
template="%(expressions)s AT TIME ZONE %(tz_info)s"
Run Code Online (Sandbox Code Playgroud)
该函数适用于简单的情况,我直接将时区字符串传递到函数中,如下所示:
MyModel.objects.filter(timefield__gte=DateTimeInTimezone(Now(), tz_info='EST'))
Run Code Online (Sandbox Code Playgroud)
但是,它不适用于更复杂的情况,即时区是在模型的某些字段上定义的。考虑以下人为的示例:
class User(models.Model):
time_zone = models.CharField()
class Meeting(models.Model):
users = models.ManyToManyField(User, related_name='meetings')
start_time = models.DateTimeField() # in UTC
end_time = models.DateTimeField() # in UTC
Run Code Online (Sandbox Code Playgroud)
要回答“今天当地时间中午 12 点哪些用户将参加会议?”这一问题,我需要此查询集的一些变体:
noon_utc = ...
User.objects.filter(
meetings__start_time__lte=DateTimeInTimezone(noon_utc, tz_info=F('time_zone')),
meetings__end_time__gt=DateTimeInTimezone(noon_utc, tz_info=F('time_zone'))
)
Run Code Online (Sandbox Code Playgroud)
然而,按照目前的编写,DateTimeInTimezone将只是将字符串注入F('time_zone')到 sql 中,而不是解析该字段。
是否可以向此函数添加对F 表达式的支持?我还应该考虑其他方法吗?
django django-orm django-queryset django-postgresql django-1.10
我有 and object,QuerySet其中包含这个对象。我需要获取下一个和上 object一个QuerySet。
我怎样才能做到这一点?
我可以这样进行下一步:
next = False
for o in QuerySet:
if next:
return o
if o==object:
next = True
Run Code Online (Sandbox Code Playgroud)
但我认为在巨大的QuerySets 上这是非常缓慢且低效的方法。
您知道更好的解决方案吗?
我有一个像 Django 模型。它存储了一段时间内发生的总交易。
class Transaction(models.Model):
amount = models.FloatField()
seller = models.ForeignKey(User, related_name='sells', on_delete=models.CASCADE)
buyer = models.ForeignKey(User, related_name='purchased', on_delete=models.CASCADE)
created_at_date = models.DateField(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)
是我怎样才能找到每天的交易总额。对于每一天,它应该计算当天所有交易的总和。
例如,我需要在过去 7 天内执行此操作。
例如,我有一个这样的模型:
class Transaction(models.Model):
amount = models.FloatField()
seller = models.ForeignKey(User, related_name='sells', on_delete=models.CASCADE)
buyer = models.ForeignKey(User, related_name='purchased', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)
我想按每个日期分隔的所有交易分组并找到其中一些。如果created_at是类型,DateField我可以通过这个查询简单地找到每天的计数:
Transaction.objects..values('created_at').annotate(count=Count('created_at_date')
Run Code Online (Sandbox Code Playgroud)
但这不起作用 DateTimeFeilds。
我的问题是如何找到此模型类型每个日期的交易总数。
这是这个问题的后续内容。虽然我可以在原始 SQL 中编写非二进制 LIKE 查询,例如 - SELECT COUNT(*) FROM TABLE WHERE MID LIKE 'TEXT%',但我想知道是否可以通过 Django ORM 来实现。
和似乎startswith都contains在使用二进制模式搜索。
我有很多 .annotate 有点复杂的 django 查询:
query = ModuleEngagement.objects.filter(course_id=course_id)\
.values('username')\
.annotate(
videos_overall=Sum(Case(When(entity_type='video', then='count'), output_field=IntegerField()))) \
.annotate(
videos_last_week=Sum(Case(When(entity_type='video', created__gt=seven_days_ago, then=1),
output_field=IntegerField()))) \
.annotate(
problems_overall=Sum(Case(When(entity_type='problem', then='count'), output_field=IntegerField()))) \
.annotate(
problems_last_week=Sum(Case(When(entity_type='problem', created__gt=seven_days_ago, then='count'),
output_field=IntegerField()))) \
.annotate(
correct_problems_overall=Sum(Case(When(entity_type='problem', event='completed', then='count'),
output_field=IntegerField()))) \
.annotate(
correct_problems_last_week=Sum(Case(When(entity_type='problem', event='completed',
created__gt=seven_days_ago, then='count'),
output_field=IntegerField()))) \
.annotate(
problems_attempts_overall=Sum(Case(When(entity_type='problem', event='attempted', then='count'),
output_field=IntegerField()))) \
.annotate(
problems_attempts_last_week=Sum(Case(When(entity_type='problem', event='attempted',
created__gt=seven_days_ago, then='count'),
output_field=IntegerField()))) \
.annotate(
forum_posts_overall=Sum(Case(When(entity_type='discussion', then='count'),
output_field=IntegerField()))) \
.annotate(
forum_posts_last_week=Sum(Case(When(entity_type='discussion', created__gt=seven_days_ago, then='count'),
output_field=IntegerField()))) \
.annotate(
date_last_active=Max('created'))
Run Code Online (Sandbox Code Playgroud)
annotate 是否接受字典作为参数,以便我可以将所有注释移动到其中?如果是这样,语法是什么?
我有这样的东西:model.objets.values('id', 'name')它返回一个以“id”和“name”为键的字典列表。有没有办法可以将自定义名称传递给这些键?类似的东西:model.objets.values('id', 'name').column_names('main id', 'name of de thing I want')?
我正在使用 python + django
这里我们使用聚合计算
>>> avg = Book.objects.aggregate(average_price=Avg('price'))
{'average_price': 34.35}
Run Code Online (Sandbox Code Playgroud)
但是为什么我们不在上面使用下面的概念。
a = Books.objects.all()
Avg = sum([x.price for x in a])/len(a)
34.35
Run Code Online (Sandbox Code Playgroud)
我想知道使用聚合而不是第二个过程。
django ×10
django-orm ×10
python ×4
aggregate ×1
django-1.10 ×1
django-2.1 ×1
django-admin ×1
mysql ×1
orm ×1