繁体   English   中英

如何保存从QuerySet返回的随机项目。 Django,Python

[英]How to save a random item returned from QuerySet. Django, Python

我正在尝试保存QuerySet中返回的项目,以便稍后它将在下一个模板中显示所有已保存的项目。 但是我不知道该怎么办?

1.)如何将“快照”正确发送到数据库?

我的models.py

class City(models.Model):
    name = models.CharField(max_length=100)

class User(models.Model):
    name = models.CharField(max_length=100)
    city = models.ForeignKey(City, on_delete=models.CASCADE)

class UserDecision(models.Model):
    decision = models.BooleanField(default=False)
    relation = models.ManyToManyField(User)

我的views.py

from django.shortcuts import render
from .models import User
from .forms import UserDecisionForm

def random_views(request):
    shot = User.objects.all().order_by('?')[:1]

    #Begining form here
    if request.method == "POST":
        form = UserDecisionForm(request.POST)
        if form.is_valid():
            decision = form.save(commit=False)
            decision.relation = shot #<--- how to pass this element to the database automatically. According to the result returned from the QuerySet query.
            decision.save()
    else:
        form = UserDecisionForm()

    # other elements
    context = {'shot': shot, 'form':form }
    return render(request, 'index.html', context)

forms.py

from django import forms

from .models import UserDecision

class UserDecisionForm(forms.ModelForm):
    class Meta:
        model = UserDecision
        fields = ('decision',)

UPDATE

简短版:尝试decision.relation = [shot]

长版:
decision.relation将返回相关的经理实例 一个类似于<model>.objects返回的管理器,除了它管理与实例decision相关的所有记录外。
需要说明的是decision.relation实际上是一个数据描述符 ,一个对象(这里的特殊属性decision ),允许一些巧妙的小把戏在访问( __get__ )或改变( __set__ )的属性:
访问属性relation ,将通过描述符的__get__方法为您提供上述管理器实例。

设置属性时(使用__set__ decision.relation = some_variable ),描述符使用其__set__方法,如下所示:

# django.db.models.fields.related_descriptors.py: 518
def __set__(self, instance, value):
    """
    Set the related objects through the reverse relation.

    With the example above, when setting ``parent.children = children``:

    - ``self`` is the descriptor managing the ``children`` attribute
    - ``instance`` is the ``parent`` instance
    - ``value`` is the ``children`` sequence on the right of the equal sign
    """
    ...
    manager = self.__get__(instance)
    manager.set(value)

因此,当您编写__set__ decision.relation = shot从而调用上面的__set__方法时,描述符将为该关系创建正确的管理器,然后在其上调用set(shot)
最后, set()期望模型实例具有可迭代性,但是您只给了它一个模型实例。


我不确定这是否会真正回答您的问题,但是我不想在开篇文章中添加冗长的注释,并且在解释代码时答案更加清晰。

values()将返回查询中对象的字典表示形式的查询集。 您可以使用这些词典为这些对象制作相同的副本。

user_data = User.objects.order_by('?').values()[:1][0]
user_data[User._meta.pk.name] = None # We cannot use an existing pk for the copy
copied_user = User.objects.create(**user_data)
return render(request, 'index.html', {'shot':copied_user})

在下一个视图中,您不清楚这些副本正在做什么。 如果您只想显示一个复制的对象,以避免任何人实际对原始对象进行更改,然后在离开视图后又再次丢弃这些副本(以免最终得到一个充满副本的表),则最好更改模板/使用简单形式仅首先显示原始数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM