Last active
December 14, 2020 12:57
-
-
Save akhsiM/3e0f5a63044da3ceb4ba8285e2c1210d to your computer and use it in GitHub Desktop.
Django snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Hello World |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
class DateInput(forms.DateInput): | |
input_type = 'date' | |
def __init__(self, **kwargs): | |
kwargs['format'] = '%d-%m-%y' | |
super().__init__(**kwargs) | |
class UserProfileForm(ModelForm): | |
bdate = forms.DateField() | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.fields['bdate'].widget = DateInput() | |
class Meta: | |
model = UserProfile | |
fields = ('ph_home', 'ph_mobile', 'ph_work' ,'addr_street', | |
'addr_city', 'addr_state', 'addr_postcode', 'addr_country', | |
'bdate', 'stu_number') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Put this in a Child Class of ModelForm | |
def update_instance(self, instance, commit=True): | |
# This function can be used in custom ModelForm to perform partial update | |
# to an object. | |
for field in instance._meta.fields: | |
if field.attname in self.fields: | |
setattr( | |
obj=instance, | |
name=field.attname, | |
value=self.cleaned_data[field.attname] | |
) | |
if commit: | |
try: | |
instance.save() | |
except Exception as e: | |
print(e) # Change this to log | |
return False | |
return instance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add form-control class to all Django Form fields | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
for visible in self.visible_fields(): | |
visible.field.widget.attrs['class'] = 'form-control' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def human_readable_size(self, object): | |
num = object.size | |
for unit in ['','K','M','G','T','P','E','Z']: | |
if abs(num) < 1024.0: | |
return "%3.1f%s%s" % (num, unit, "B") | |
num /= 1024.0 | |
return "%.1f%s%s" % (num, 'Yi', "B") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment