模型的表单实例给出id = None Django

时间:2022-06-27 19:22:36

I'm misunderstanding something! If my model is not saved, it does not have an id associated with it. So if I have something like this:

我误会了什么!如果我的模型未保存,则它没有与之关联的ID。所以,如果我有这样的事情:

views.py (inserting or editing existing info uses the same modelform)

views.py(插入或编辑现有信息使用相同的模型)

def insert_or_modify(request, id=None):
    if id is not None:
         book = BookModel.objects.get(pk=id)
    else:
         book = BookModel()

    if request.method == 'POST':
        form = BookInfoForm(request.POST, request.FILES, instance=book)
        if form.is_valid():
            form.save()

    ....

    return render_to_response(...)

I also have an image and use upload_to for the imagefield. There are two problems: id is None and I'm not sure how to manipulate/save the instance=book so that I would actually get an id. The second problem is the location I save my data to is way off. Even though the template tag book.img.url has the desired location of the book at http:127.0.0.1:8000/folder1/media/id/, the actual location is somewhere else:

我还有一个图像,并使用upload_to作为图像域。有两个问题:id是None,我不知道如何操作/保存instance = book,这样我才真正得到一个id。第二个问题是我保存数据的位置是关闭的。即使模板标签book.img.url在http:127.0.0.1:8000 / folder1 / media / id /上具有所需的书籍位置,实际位置也在其他位置:

Where I want to save my image to:

我想将图像保存到的位置:

/project/folder1/media/id/

where id is the book id.

其中id是书籍ID。

What I actually get:

我真正得到的是:

/project/id/      

(But 'id' becomes 'None' since it doesn't exist!)

(但'id'变为'None',因为它不存在!)

My previous code worked. It would save to the correct location, but with this current code, it doesn't work. So the saving issue doesn't seem like it's due to settings.py since it worked previously.

我以前的代码工作。它将保存到正确的位置,但使用此当前代码,它不起作用。所以保存问题似乎不是由于settings.py,因为它以前工作。

EDIT: removed non-code from code formatting area

编辑:从代码格式区域中删除非代码

EDIT: I found out why I wasn't saving to the correct location. As it turned out, I forgot to uncomment something when I last modified settings.py. Saving to the location works now! Sorry guys!

编辑:我发现为什么我没有保存到正确的位置。事实证明,当我上次修改settings.py时,我忘记取消注释。保存到位置现在工作!对不起大家!

EDIT: I think the id=None problem is caused by form.save(). If I avoid doing that and just save to the model directly, I don't have this problem.

编辑:我认为id = None问题是由form.save()引起的。如果我避免这样做并直接保存到模型中,我就没有这个问题。

3 个解决方案

#1


4  

Id assigns only on saving objects when you use autoincrement id field (default). You can save item before handling image, and then save image.

当您使用autoincrement id字段时,Id仅分配保存对象(默认)。您可以在处理图像之前保存项目,然后保存图像。

May be you can not worry about image name - becouse django file storages dont have troubles with same image names. So if you just save file "image.png", and then save another file with name "image.png" - then it will be saved as "image_1.png"

也许你不用担心图像名称 - 因为django文件存储没有相同图像名称的麻烦。因此,如果您只保存文件“image.png”,然后保存名为“image.png”的另一个文件 - 那么它将保存为“image_1.png”

def add_or_create(request, item_id=None):
    item = get_object_or_404(BookModel, id=item_id) if item_id else None
    form = BookInfoForm(request.POST or None, request.FILES or None, instance=book) # assume it is ModelForm

    if form.is_valid():
        book = form.save()

#2


1  

For the first part:

第一部分:

def insert_or_modify(request, id=None):
    if id:
        book = BookModel.objects.get(pk=id)
        if request.method == 'POST':
            form = BookInfoForm(request.POST, request.FILES, instance=book)
            if form.is_valid():
                save_book = form.save()
                # use save_book as your instance of BookModel
        ....
    else:
        if request.method == 'POST':
            form = BookInfoForm(request.POST, request.FILES)
            if form.is_valid():
                save_book = form.save()
                # use save_book as your instance of BookModel
        ....

save_book = form.save() allows you to then use save_book as your saved instance of BookModel, and save_book.id is its id.

save_book = form.save()允许您使用save_book作为BookModel的已保存实例,save_book.id是其id。

#3


0  

def create_id(instance,some_id=None):

if some_id is None:
    obj=Post.objects.first()
    new_id=obj.id
    new_id+=1
    return new_id
else:
    return some_id

def pre_save_post_receiver(sender, instance, *args, **kwargs):

def pre_save_post_receiver(sender,instance,* args,** kwargs):

if not instance.id:
    instance.id = create_id(instance)

pre_save.connect(pre_save_post_receiver, sender=Post)

#1


4  

Id assigns only on saving objects when you use autoincrement id field (default). You can save item before handling image, and then save image.

当您使用autoincrement id字段时,Id仅分配保存对象(默认)。您可以在处理图像之前保存项目,然后保存图像。

May be you can not worry about image name - becouse django file storages dont have troubles with same image names. So if you just save file "image.png", and then save another file with name "image.png" - then it will be saved as "image_1.png"

也许你不用担心图像名称 - 因为django文件存储没有相同图像名称的麻烦。因此,如果您只保存文件“image.png”,然后保存名为“image.png”的另一个文件 - 那么它将保存为“image_1.png”

def add_or_create(request, item_id=None):
    item = get_object_or_404(BookModel, id=item_id) if item_id else None
    form = BookInfoForm(request.POST or None, request.FILES or None, instance=book) # assume it is ModelForm

    if form.is_valid():
        book = form.save()

#2


1  

For the first part:

第一部分:

def insert_or_modify(request, id=None):
    if id:
        book = BookModel.objects.get(pk=id)
        if request.method == 'POST':
            form = BookInfoForm(request.POST, request.FILES, instance=book)
            if form.is_valid():
                save_book = form.save()
                # use save_book as your instance of BookModel
        ....
    else:
        if request.method == 'POST':
            form = BookInfoForm(request.POST, request.FILES)
            if form.is_valid():
                save_book = form.save()
                # use save_book as your instance of BookModel
        ....

save_book = form.save() allows you to then use save_book as your saved instance of BookModel, and save_book.id is its id.

save_book = form.save()允许您使用save_book作为BookModel的已保存实例,save_book.id是其id。

#3


0  

def create_id(instance,some_id=None):

if some_id is None:
    obj=Post.objects.first()
    new_id=obj.id
    new_id+=1
    return new_id
else:
    return some_id

def pre_save_post_receiver(sender, instance, *args, **kwargs):

def pre_save_post_receiver(sender,instance,* args,** kwargs):

if not instance.id:
    instance.id = create_id(instance)

pre_save.connect(pre_save_post_receiver, sender=Post)