【问题标题】:django: foreign key issues when creating a model objectdjango:创建模型对象时的外键问题
【发布时间】:2023-01-02 15:32:40
【问题描述】:

我正在尝试将一行数据写入数据库,并以表格形式收集数据。我需要使用两个外键,其中一个导致创建失败,尽管我无法弄清楚原因:

这是我的模型:

def upload_path(instance,file):
    file_dir = Path(file).stem
    print('usr',instance.user.id)
    path = '{}/{}/{}/{}'.format(instance.user.id,"projects",file_dir,file)

    return path


class BuildingFilesVersions(models.Model):
    version_id = models.AutoField(primary_key=True)
    building_id = models.ForeignKey(Building, on_delete=models.CASCADE,related_name='building_id_file')
    user = models.ForeignKey(Building, on_delete=models.CASCADE,related_name="user_file")
    created_at = models.DateTimeField(auto_now_add=True, blank=True)
    description = models.TextField(max_length=200, blank=True, null=True)
    modification_type = models.CharField(choices=WORK_TYPE_CHOICES, max_length=200, blank=True, null=True)
    filename = models.CharField(max_length=200, blank=True, null=True)
    file = models.FileField(upload_to=upload_path, null=True, blank=True)

这是我的观点:

@login_required
@owner_required
def RegisterFileView(request,pk):
    form = AddBuildingFileForm()
    if request.method == 'POST':
        form = AddBuildingFileForm(request.POST,request.FILES)
        if form.is_valid():
            description = form.cleaned_data["description"]
            modification_type = form.cleaned_data["modification_type"]
            filename = form.cleaned_data["modification_type"]
            file =  request.FILES['file'].name

            BuildingFilesVersions.objects.create(building_id_id=pk,
                                                 user_id=request.user,
                                                 description=description,
                                                 modification_type=modification_type,
                                                 filename=filename,
                                                 file=file)

            return redirect('home')
        else:
            form = AddBuildingFileForm()
    context = {'form':form}
    return render(request, 'building_registration/register_file.html', context)

让我感到困惑的是错误是Field 'building_id' expected a number but got <SimpleLazyObject: <User: Vladimir>> ,即使pk返回正确的building_id

谁能看到我搞砸的地方?

【问题讨论】:

    标签: python django


    【解决方案1】:

    要访问外键的 ID 添加双下划线

    BuildingFilesVersions.objects.create(building_id__id=pk,
                                                     user__id=request.user.id,
                                                     description=description,
                                                modification_type=modification_type,
                                                     filename=filename,
                                                     file=file)
    

    See the Docs

    【讨论】:

    • 感谢您的回答。还是不行。做你的建议输出BuildingFilesVersions() got an unexpected keyword argument 'building_id__id
    猜你喜欢
    • 2021-10-30
    • 2011-05-10
    • 2015-05-20
    • 2016-06-11
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    相关资源
    最近更新 更多