What is the work of instance & filename which is passing as parameter?

23 views
Skip to first unread message

Tanjina Sharmin

unread,
Sep 3, 2021, 8:25:58 AM9/3/21
to Django users
def path_and_renamecsv(instance, filename):
upload_to = 'attendancesheet'
ext = filename.split('.')[-1]
# get filename
if instance.pk:
filename = '{}.{}'.format(instance.pk, ext)
else:
# set filename as random string
filename = '{}.{}'.format(uuid4().hexq, ext)
# return the whole path to the file
return os.path.join(upload_to, filename)


Kasper Laudrup

unread,
Sep 3, 2021, 4:14:52 PM9/3/21
to django...@googlegroups.com
Not sure what you mean by "work of", but Python is a dynamically typed
language so you can really pass whatever type you want to the function.

This line:

> ext = filename.split('.')[-1]

looks like the filename parameter is most likely expected to be a string
and this line:

> if instance.pk:

indicates that instance is something that is expected to have a pk
member in some (most?) cases.

What exactly is this function and where did you get it from?

I'm assuming you didn't write it yourself.

A bit more context would make it a lot easier for someone to help you.

Kind regards,

Kasper Laudrup

OpenPGP_signature

Sky Lord

unread,
Sep 3, 2021, 4:23:50 PM9/3/21
to django...@googlegroups.com
So right here what does the filename changes to?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cc8c6fbc-2d66-49eb-80c5-4e8b85d66e34n%40googlegroups.com.

Hella thor

unread,
Sep 11, 2021, 7:29:59 AM9/11/21
to django...@googlegroups.com
For Chinese programmers,it's money

Tanjina Sharmin <tanjinash...@gmail.com> 于2021年9月3日周五 下午8:25写道:
Reply all
Reply to author
Forward
0 new messages