class EditForm(forms.ModelForm):
category = forms.ModelChoiceField(
queryset=Category.objects.all(),
label='Categories',
widget=forms.Select()
)
class Meta:
model = Item
fields = ('category', 'mode', 'type', 'delivery' )
Category
из моделей.models.py
class Audit(models.Model):
"""
A common class for auditing the models' instances.
"""
created_by = models.ForeignKey(
User,
related_name='%(app_label)s_%(class)s_created_related',
verbose_name=_(u'Создал'),
on_delete=models.SET_NULL,
blank=True, null=True)
changed_by = models.ForeignKey(
User,
related_name='%(app_label)s_%(class)s_changed_related',
verbose_name=_(u'Изменил'),
on_delete=models.SET_NULL,
blank=True, null=True)
created = models.DateTimeField(
_(u'Дата создания'), auto_now_add=True)
changed = models.DateTimeField(
_(u'Дата изменения'), auto_now=True)
class Meta:
abstract = True
admin.py
class AuditAdmin(admin.ModelAdmin):
"""
Admin class for audit fields
"""
readonly_fields = (
'created_by',
'changed_by',
'created',
'changed')
fieldsets_preset = (
_(u'Прочее'), {
'fields': (
'created_by',
'created',
'changed_by',
'changed'
)
})
actions_on_top = True
def save_model(self, request, obj, form, change):
if not change:
obj.created_by = request.user
obj.changed_by = request.user
obj.save()
entries__set
is_published=True
, когда сами же пишете, что нужны неопубликованные... class BookAdmin(admin.ModelAdmin):
list_display = ("pk", "get_title_or_nothing")
def get_form(self, request, obj=None, **kwargs):
if obj.type == "1":
self.exclude = ("title", )
form = super(BookAdmin, self).get_form(request, obj, **kwargs)
return form
class CarAdmin(admin.ModelAdmin):
list_display = ("pk", "model", "vin")
def get_form(self, request, obj=None, **kwargs):
if 'Red' not in obj.color:
self.exclude = ("field1", "field2")
form = super(CarAdmin, self).get_form(request, obj, **kwargs)
return form
from django.db import models
class B(models.Model):
data = models.ForeignKey('app1.A')
from django.db import models
from django.apps import apps
class B(models.Model):
data = models.ForeignKey('app1.A')
def some_method(self):
a_class = apps.get_model('app1', 'A')
a_object = a_class.objects.get(id=1)
...
from django.contrib.auth.models import User
class Customer(models.Model):
user = models.OneToOneField(User)
shops = models.ManyToManyField(Shops)
class CustomerInline(admin.StackedInline):
model = Customer
fk_name = 'user'
can_delete = False
filter_horizontal = ('shops', )
class CustomerAdmin(UserAdmin):
inlines = (CustomerInline, )
admin.site.unregister(User)
admin.site.register(User, CustomerAdmin)