简体   繁体   English

在 Django 中创建时,如何将模型分配给所有用户?

[英]How can I assign a model to all users upon creation in Django?

I have this model:我有这个模型:

models.py模型.py

    class Upload_model(models.Model):
        content=models.CharField(max_length=200)
        user_name=models.ForeignKey(User)

forms.py表格.py

class upload_form(ModelForm):
    class Meta:
        model=Upload_model
        fields=[“content”]

views.py视图.py

I'm trying to write a function that iterates the creation of the Upload_model to all Users independently.我正在尝试编写一个函数,该函数可以独立地向所有用户迭代 Upload_model 的创建。 But it won't work.但它不会工作。

def upload_all_user_models(request):
    if request.method==”POST”:
        form=upload_form(request.POST, request.FILES)
        instance=form.save(commit=False)
        instance.user_name=User.objects.all()
        return redirect(“upload_all_user_models”)
    else:
        form=upload_form()
    return render(request,”uploadpage.html”,{“form”:form})

I want the Upload_model to be assigned to all users upon creation but I seem to be getting a ValueError.我希望在创建时将 Upload_model 分配给所有用户,但我似乎收到了 ValueError。 How can I solve this?我该如何解决这个问题?

I believe when you use instance.user_name=User.objects.all() , you're assigning the instance.user_name to a queryset of User rather than each individual users (ie instance.user_name=[queryset<id=1, id=2>] ).我相信当您使用instance.user_name=User.objects.all() ,您将instance.user_name分配给 User 的查询集而不是每个单独的用户(即instance.user_name=[queryset<id=1, id=2>] )。 You need to set up conditional statements that determine if each user doesn't have the Upload_model, then add and save Upload_model to the user.您需要设置条件语句来确定每个用户是否没有 Upload_model,然后将 Upload_model 添加并保存到用户。

What you're trying to create is going to be complicated due to the order in which model gets created first: the upload_model or the user.由于首先创建模型的顺序:upload_model 或用户,您尝试创建的内容将变得复杂。

Let's say you have 5 users and you create an Upload_model, saving the Upload_model to the 5 users.假设您有 5 个用户并且您创建了一个 Upload_model,将 Upload_model 保存到 5 个用户。 What's going to happen if user #6 comes along?如果用户#6 出现会发生什么?

I'm sure there's a way you can configure the code to make it how you want it, but at the end of it all, your question is quite vague but all in all you're getting a ValueError because you're trying to assign user_name not to each user, but to a list of users.我确定有一种方法可以配置代码以使其成为您想要的方式,但最后,您的问题很模糊,但总而言之,您收到了 ValueError 因为您正在尝试分配user_name 不是分配给每个用户,而是分配给用户列表。

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

相关问题 如何从脚本自动创建 django model 实例? - How can I automate the creation of django model instances from a script? 如何根据创建时间在django管理页面中订购本地django用户? - How can I order the native django users in the django admin page based on creation time? 如何构造一个 Django model 其字段依赖于另一个模型的字段 - How can I construct a Django model whose fields are dependent upon another model's fields 我如何序列化Django模型中的所有模型字段 - how can i serialize all model fields inside model in django 如何显示Django中所有用户的日程安排? - How can I show daily schedules of all users in Django? 如何将一些(但不是全部)模型与使用 Django 的用户联系起来? - How can I relate some, but not all, models to users using Django? 我怎样才能在template,django中获取所有用户图像 - How can I get all users images in template,django 如何将特定用户分配给用户上传的文件,以便他们可以对其进行修改/删除(Django + Apache) - How do I assign specific users to a user-uploaded file so they can modify it/delete it (Django + Apache) django-如何获取与模型相关的所有外键的列表? - django - how can I get a list of all foreignkeys related to a model? 如何在Django模型中找到所有名称的最新匹配? - How can I find the newest match for all names in a Django model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM