2

Is there a way to automatically set field values for models in Django when defining the model? I need t define some values of fields automatically in my model using function. my function get input image path calculate and I need that calculation results to define my database fields in Django.

first to I want is something like this :

my view :

def myview(request):
    uploadimages = UploadImagesForm(request.POST or None, request.FILES or None)
    if uploadimages.is_valid():
        # Get the images that have been browsed
        if request.FILES.get('multipleimages', None) is not None:
            images = request.FILES.getlist('multipleimages')
            for image in images:
                MyModel.objects.create(field1=request.user,field2=image)

that doesn't work because to work my function need first to upload image in server to get the path to work.

any idea how to define my model automate using my function ?

update

instance = MyModel.objects.create(user=request.user, upload=image)
                instance.field1 = name
                instance.field2 = myvalue
                instance.field3 = myvalue2
                instance.field4  = myvalue3
                instance.field5  = myvalue4
                instance.save()

error in this code is the my function cant understand the image path to create the calculation to set the values in fields.

if I use this :

MyModel.objects.create(user=request.user, upload=image)
instance = MyModel.objects.create(user=request.user, upload=image)
                instance.field1 = name
                instance.field2 = myvalue
                instance.field3 = myvalue2
                instance.field4  = myvalue3
                instance.field5  = myvalue4
                instance.save()

that work but create me duplicates in database .

1
  • 1
    .create() method is creating a record in database without .save() method. Just define instance = MyModel(user=request.user, upload=image), other fields and then call .save(). In this way, you avoid the problem of duplicating. Commented Aug 29, 2017 at 20:34

1 Answer 1

1

You can try:

instance = MyModel.objects.create(field1=request.user, field2=image)
instance.field3 = myfunc(image)
instance.field4 = myfunc(image)
instance.save()
Sign up to request clarification or add additional context in comments.

6 Comments

ok but is the same just replace my MyModel.objects.create with your code
and remove old code, please, leave only clear new code
you try override your real code for the question, so we have an trouble with the understanding, double creating can be the solution, may be you can show real code?
look create it for any image upload double insert in my database first insert is complete,all fields is correct(that mean work my function) and the second insert have only the path (field2=image) and all others fields is empty..help this ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.