Is there any way to use a Form Wizard in Admin interface for add/edit Models.
(using Django 1.5.2)
for example:
--models.py--
class AModel(models.Model):
fieldA = models.CharField(max_length=64)
fieldB = models.CharField(max_length=64)
--admin.py--
class Form1(ModelForm):
class Meta:
model = AModel
fields = ('fieldA',)
class Form2( ModelForm ):
class Meta:
model = AModel
fields = ('fieldB',)
.... something add for make this two forms in one multipage admin form , is that possible? or any other way to do the same job.
Thanks in advance.
It isn't possible by using admin module. The only way is either some external plugins or you need to create custom admin views. Google around..
Related
I'm working on a project developed in Python 2.7 and Django 1.11.
I'm trying to show in admin page two fields passing through a ManyToMany field.
Here the models:
class ModelZero(Model):
# some fields
mtm_field = models.ManyToManyField(to="ModelOne", through="ModelTwo")
class ModelOne(Model):
# some fields
field_1_1 = models.CharField(unique=True, max_length=200)
field_1_2 = models.BooleanField(default=True)
class ModelTwo(Model):
# some fields
field_2_1 = models.ForeignKey('ModelOne', on_delete=models.CASCADE)
field_2_2 = models.BooleanField(default=True)
In the ModelZero admin page I want to show some fields from the ModelZero itself plus field_2_1 and field_2_2 from ModelTwo.
More in detail, the field_2_1 should be present using a custom widget.
Please note that ModelZeroAdmin is an inline ones.
Here the admin page:
class ModelZeroAdmin(DynamicRawIDMixin, admin.TabularInline):
model = ModelZero
fields = ('some', 'fields', 'field_2_2')
form = forms.ModelZeroForm
def field_2_2(self, obj):
return obj.mtm_field.through.field_2_2
Here the form:
class ModelZeroForm(ModelForm):
class Meta:
widgets = {
"mtm_field.through.field_2_1": dal.autocomplete.ModelSelect2Multiple(
url="my-autocomplete-url"
)
}
In this way i have two errors:
it's not possible add custom fields (field_2_2) in the fields tuple
custom widget is not showed
Is there a way to achieve this goal using this models structure?
I don't have experience with older Django version, but if I am not mistaken the syntax for a related field in admin interface would be something like :mtm_field__field_2_2.
I am trying to understand the process of generating generic form views in django. I have a generic view class with just
class BookUpdate(UpdateView):
model = Book
fields = [ 'name',
'pages',
'categorys'
]
which automatically generates a working html form from my model data. But now, I want to modify the field that is shown for categorys, is there any way to do this, or do I have to create a complete working BookForm class and custom BookUpdate class? Here its just 3 fields, but in my real case there are maybe 15 fields that I would need to code by myself, just because of a tiny change in the category field.
Cant I just overwrite the single field, using any class method?
You can either specify fields or form_class in your generic class-based view. With fields, Django will use a modelform_factory to generate the form. There's not much you can customise then.
You should create a BookForm class so that you can customise the fields. In your BookUpdate view, you only need to remove fields and add form_class = BookForm. Here I'm customising the widget for categorys and overriding the form field for pages:
def BookUpdate(UpdateView):
model = Book
form_class = BookForm
def BookForm(ModelForm):
pages = MyCustomPagesField()
class Meta:
model = Book
fields = '__all__'
widgets = {'categorys': MyCustomWidget()}
Note that you don't have to specify all fields, you can use "__all__" to have all fields or you can set exclude = [<list fields to exclude>] to just exclude a couple.
You don't have to code the fields yourself. But there is a small amount of work to do, as there isn't a method to override.
What you need to do is define a custom form. Since that will be a ModelForm, it will use the same logic to automatically create its fields based on the model. You can then override the definition of one of them.
class BookForm(forms.ModelForm):
categorys = forms.ModelMultipleChoiceField(custom_attributes_here...)
class Meta:
model = Book
fields = ["name", "pages", "categorys"]
And now tell your view to use that form:
class BookUpdate(UpdateView):
form_class = BookForm
I'm using django-rest-framework and python-social-auth in my Django project.
Here is the serializer class of UserSocialAuth model in my project
class SocialAuthSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.CharField()
class Meta:
model = UserSocialAuth
fields = ('id', 'provider')
Then I want to an additional field UserSocialAuth.extra_data['login'] to above Serializer, the traditional way should be
class UserSocialAuth(AbstractUserSocialAuth):
def login:
return self.extra_data['login']
class SocialAuthSerializer(serializers.HyperlinkedModelSerializer):
login = serializers.CharField(source='login')
...
fields = ('id', 'provider', 'login')
The problem is that UserSocialAuth is belong to python-social-auth, I have to change the code of python-social-auth app directly to add def login:, so how can I add the additional field to the existing model UserSocialAuth without touching the code of python-social-auth.
I just find that I can use SerializerMethodField here, no need to change the raw class UserSocialAuth, just add one more field to the serializer like this:
class SocialAuthSerializer(serializers.HyperlinkedModelSerializer):
login = serializers.SerializerMethodField()
def get_login(self, obj):
return obj.extra_data['login']
I am new to Django and Python and wanted to ask you how I can get models and forms in one class. I want to use Radio Buttons, EmailField, DateTimeField so I need really both (models and forms) :
class Post(models.Model):
BOOL_CHOICES = ((True, 'male'), (False, 'female'))
name= models.CharField(max_length=255)
gender= models.BooleanField(choices=BOOL_CHOICES)
Of course I can not write class Post(models.Model) if I use models and forms but I dont know what to do to dispaly both in my view.
Thanks in advance
I don't know why you think you need to do this in the model. If you want to customize the form representation of a model, then you simply define a custom modelform and override the relevant field:
class PostForm(forms.ModelForm):
gender = forms.TypedChoiceField(choices=BOOL_CHOICES, widget=RadioSelect,
coerce=bool)
class Meta:
model = Post
I have a model called "Activity" in my django app. in the admin interface, it appears on the screen as "Activitys". how can I override the label on the admin page to make it "Activities" instead?
I see in the archives how to do this for a field, but not for a model itself. thanks!
class MyModel(models.Model):
# your fields....
class Meta:
verbose_name = 'Activity'
verbose_name_plural = 'Activities'