I have an 'order' Model:
class Order(models.Model):
date_time=models.DateTimeField()
# other stuff
And I'm using Django ModelForm class to render a form, but I want开发者_运维百科 to display date and time widgets separately. I've came up with this:
class Form(forms.ModelForm):
class Meta:
model = Order
exclude = ('date_time',)
date = forms.DateField()
time = forms.TimeField()
The problem is that I want to put these fields somewhere between 'other stuff'
You can use:
class Meta:
fields = [ ..., 'date', 'time', ... ]
See the docs: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-order-of-fields
Adding to Lukasz answer, in Django 2.0 the complete admin form registration with field ordering would look like the following:
models.py
class Order(models.Model):
date_time = models.DateTimeField()
# other fields
admin.py
class OrderForm(forms.ModelForm):
datef = forms.DateField()
timef = forms.TimeField()
class Meta:
model = Order
exclude = ['date_time']
fields = (
'datef',
'timef',
# other fields
)
class OrderAdmin(admin.ModelAdmin):
form = OrderForm
admin.site.register(Order, OrderAdmin)
Note that date and time fields are defined in ModelForm class and not in Meta class.
精彩评论