Model form instance gives id = None Django

I misunderstand something! If my model is not saved, it does not have an identifier associated with it. Therefore, if I have something like this:

views.py (pasting or editing existing information uses the same model)

 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 image field. There are two problems: id is None, and I'm not sure how to manipulate / save instance=book so that I really get the identifier. The second problem is that the location where I save my data is far away. Although the book.img.url template book.img.url has the desired book location at http:127.0.0.1:8000/folder1/media/id/ , the actual location is somewhere else:

Where I want to save my image:

 /project/folder1/media/id/ 

where id is the identifier of the book.

What I actually get:

 /project/id/ 

(But 'id' becomes 'None' because it does not exist!)

My previous code worked. It will save in the right place, but this will not work with this current code. So the save problem is not like this because of settings.py, as it worked previously.

EDIT: Remote Necode from Code Formatting Area

EDIT: I found out why I did not save the data in the right place. As it turned out, I forgot to uncomment something the last time I modified settings.py. Saving a location now works! Sorry guys!

EDIT: I think the id = None problem is caused by form.save (). If I do not do this and just save for the model directly, I do not have this problem.

+6
source share
3 answers

The identifier is assigned only when saving objects using the auto-increment identifier field (by default). You can save the item before processing the image, and then save the image.

Maybe you can not worry about the image name - becouse django file vaults do not have problems with the same image names. Therefore, if you just save the file "image.png" and then save another file with the name "image.png" - then it will be saved as "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() 
+4
source

In 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 use save_book as a saved instance of BookModel, and save_book.id is its identifier.

+1
source

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):

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

pre_save.connect (pre_save_post_receiver, sender = Post)

0
source

Source: https://habr.com/ru/post/903879/


All Articles