How to remove unwanted stuff from ImageField - Django - django

I want to remove currently and clear field from my django form.
forms.py
class MyUserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['user_phone','user_dob','user_gender', 'user_image', ]
I am using crispy_forms also

That functionality is provided by ClearableFileInput. If you don't want it, use FileInput instead:
class MyUserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['user_phone','user_dob','user_gender', 'user_image', ]
widgets = {'user_image': forms.FileInput}

Related

How to avoid fields that has null values automatically when rendering html

I want to not to display the null value fields in template.So How can i achieve this in django?Is there any functions available to do this.
In you used Django ModeForm then do this
class ProductForm(forms.ModelForm):
class Meta:
model = ProductModel
exclude = ('selling_price','discounted_price') # this is exclude fields not render in html template
NOTE:- pass null=True, blank=True in Model
If what you are saying is that you do not want a field that has been set to null = True in your models.py to be rendered in your html template, you can use django inbulit form class to render the form and exclude whatever field is null
Here is an example of what i am saying.
class Userdetail(forms.ModelForm):
class Meta:
model = User_detail
exclude = ("name_of_null_field",) #This field won't be rendered in your html template because it is excluded.
You can read more on Django forms here Working with forms

How to add an additional field in Django model form

I am trying to include some dummy fields in my model form and using the same in model formset factory.
Model form:
class DistForm(forms.ModelForm):
dist_from_txt = forms.CharField()
...
class Meta:
model: Distance
fields = ('dist_from', 'dist_to', 'distance')
widgets = {
...
}
However, when rendered the extra field does not show up on the form.
Needlessly to mention here that I have searched (including here) and failed to find a possible solution.
Question is: How to correctly add and render extra field/s in model form?
forms.TextInput is a widget instance, not a form field instance. Use CharField:
class DistForm(forms.ModelForm):
dist_from_txt = forms.CharField()
...
class Meta:
model: Distance
fields = ('dist_from', 'dist_to', 'distance')
widgets = {
...

use `SplitArrayField` instead of `SimpleArrayField` in ModelForm

I have model with ArrayField and I want use ModelForm. Django by default use SimpleArrayField but I need SplitArrayField. I get my data from json and I use form only for validation and I don't need input widgets. (I use client side rendering)
class Profile(models.Model):
phone = ArrayField(CharField(max_length=20, validators=[some_validator]))
class ProfileForm(ModelForm):
class Meta:
model = Profile
form = ProfileForm(data={"phone":["555-5555","444-4444"]})
form.validate()
How I can use SplitArrayField in ModelForm?
I solve my problem with field_classes in Meta class:
class ProfileForm(ModelForm):
class Meta:
model = Profile
field_classes = {
'phone': SplitArrayField, # or any custom field
}
note:
SplitArrayField is not good enough for me so I create my own array form field
I think with pure django this is not possible at the moment. There is ticket which proposes the the possibility to use a SplitArrayField.
But you could use this package: django_postgres_extensions.
There you can use a SplitArrayField by defining the form_size parameter:
from django_postgres_extensions.models.fields import ArrayField
class Product(models.Model):
keywords = ArrayField(models.CharField(max_length=20), default=[], form_size=10, blank=True)

Adding custom data to django model field

I'd like to add some info to a model field to use at form rendering time. My real model has about 15 values of varying field types (adding and removing as I dev), and it does almost everything I need, so I'd rather not create custom model fields for all of them.
I'd like to do something like this:
from django.db import models
class MyModel(models.Model):
cost = models.DecimalField(max_digits=5,
decimal_places=2,
custom_info= {'glyph': 'glyphicon glyphicon-usd' }
)
And then in my form template use that glyph much like I'd use a verbose_name or help_text.
Something I learned from a post just the other day. Will defining the custom information on the form instead of the model work?
When you define formfield_callback on a forms.ModelForm it will iterate over the form fields and you can manipulate them. This comes in handy when you need to add a css class to widgets and don't want to explicitly override the field. Now you only need to put formfield_callback = modify_form_field on any forms.ModelForm where you want the custom_info to show up.
from django.db import models
def add_glyphicons(model_field):
form_field = model_field.formfield()
if isinstance(model_field, models.IntegerField):
form_field.custom_info = {'glyph': 'glyphicon glyphicon-usd'}
elif isinstance(model_field, models.CharField):
form_field.custom_info = {'glyph': 'glyphicon glyphicon-yen'}
return form_field
class MyModel(models.Model):
formfield_callback = add_glyphicons
class Meta:
model = MyModel
class MyOtherModel(models.Model):
formfield_callback = add_glyphicons
class Meta:
model = MyOtherModel

change django-tinymce content css for various object

How can I change content_css option in django-tinymce for various objects? I mean that when I use tinymce for model1 then content_css is model1.css and when for model2 then model2.css.
I use
I found out that I can pass extra arguments for tiny_mce in Meta class:
class Meta:
model = MyModel
widgets = {
'field_name': TinyMCE(mce_attrs={'content_css': "style.css"}),
}
Thanks for the szaman's reply, I'll try to describe the process for beginners in new versions of Django (1.9), how to add custom css files to the field named 'text' in the Post model in the Django's admin
Change fields' type in models.py to HTMLField
from tinymce.models import HTMLField
class Post(models.Model):
title = models.TextField(default='')
subtitle = models.TextField(default='')
text = HTMLField(default='')
In the application with the required model, add to admin.py file:
#admin.register(Post) # decorator for adding Django admin for Post
class PostAdmin(admin.ModelAdmin):
form = PostForm # attach custom form
Add form's class
from tinymce.widgets import TinyMCE
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = '__all__' # required in Django (>=1.8)
widgets = {
'text': TinyMCE(mce_attrs={'content_css': ["path_to_css_file",]}),
}