Categories
Python Answers

How to get a model’s fields in Python Django?

To get a model’s fields in Python Django, we can use the get_fields method.

For instance, we write

from django.contrib.auth.models import User

[field.name for field in User._meta.get_fields()]

to call User._meta.get_fields() to return the field names of the fields in the User model with

[field.name for field in User._meta.get_fields()]

field.name has the name for each field.

Categories
Python Answers

How to add image in an ImageField from image URL with Python Django?

To add image in an ImageField from image URL with Python Django, we can use the NamedTemporaryFile class.

For instance, we write

from django.core.files import File
from django.core.files.temp import NamedTemporaryFile

img_temp = NamedTemporaryFile(delete=True)
img_temp.write(urllib2.urlopen(url).read())
img_temp.flush()

im.file.save(img_filename, File(img_temp))

to get the create a NamedTemporaryFile object.

Then we call write with the image result we get from urllib2.urlopen(url).read() where url is the image URL.

And then we call flush to put the file into img_temp.

Then we save img_temp as the file with

im.file.save(img_filename, File(img_temp))
Categories
Python Answers

How to set file upload size limit with Python Django?

To set file upload size limit with Python Django, we can create our own function to do the check.

For instance, we write

from django.core.exceptions import ValidationError

def file_size(value): 
    limit = 2 * 1024 * 1024
    if value.size > limit:
        raise ValidationError('File too large. Size should not exceed 2 MiB.')

to create the file_size function to check if the size of the file is bigger than the limit where both are in bytes.

We raise a ValidationError if the size exceeds the limit.

Then in our model, we add that as the validator by writing

image = forms.FileField(required=False, validators=[file_size])

to create a file field with validators set to [file_size] to add file_size as the validator function for the file field.

Categories
Python Answers

How to create an object for a Django model with a many to many field?

To create an object for a Django model with a many to many field, we can get the through model from the entity we want to create the objects many to many relation for.

For instance, we write

from django.db import models

class Users(models.Model):
    pass

class Sample(models.Model):
    users = models.ManyToManyField(Users)

to add the Sample model.

Then we write

Users().save()
Users().save()

ThroughModel = Sample.users.through

users = Users.objects.filter(pk__in=[1,2])

sample_object = Sample()
sample_object.save()

ThroughModel.objects.bulk_create([
    ThroughModel(users_id=users[0].pk, sample_id=sample_object.pk),
    ThroughModel(users_id=users[1].pk, sample_id=sample_object.pk)
])

to get the users model from Sample with

ThroughModel = Sample.users.through

Then we create a Sample object with

sample_object = Sample()
sample_object.save()

Then we create the users with

ThroughModel.objects.bulk_create([
    ThroughModel(users_id=users[0].pk, sample_id=sample_object.pk),
    ThroughModel(users_id=users[1].pk, sample_id=sample_object.pk)
])
Categories
Python Answers

How to get the current URL within a Python Django template?

Sometimes, we want to get the current URL within a Python Django template.

In this article, we’ll look at how to get the current URL within a Python Django template.

How to get the current URL within a Python Django template?

To get the current URL within a Python Django template, we can use request.path or request.get_full_path.

For instance, we write

{{ request.path }}  
{{ request.get_full_path }}  

in our template to render the URL without query parameters with request.path.

And we render the full URL with the query parameters with get_full_path.

Conclusion

To get the current URL within a Python Django template, we can use request.path or request.get_full_path.