def __init__(self, *args, **kwargs): super(CategoryAdminForm, self).__init__(*args, **kwargs) import django if django.VERSION >= (1, 6, 0): rel = ManyToOneRel(None, Category, 'id') else: rel = ManyToOneRel(Category, 'id') self.fields['parent'].widget = RelatedFieldWidgetWrapper( self.fields['parent'].widget, rel, self.admin_site)
def __init__(self, *args, **kwargs): super(CategoryAdminForm, self).__init__(*args, **kwargs) if is_before_1_6: rel = ManyToOneRel(Category, 'id') else: #This may or may not be the correct fix. #This really ought to be thrown out and replaced with something #actually a part of the API. rel = ManyToOneRel(Category._meta.get_field('tree_id'), Category, 'id') self.fields['parent'].widget = RelatedFieldWidgetWrapper( self.fields['parent'].widget, rel, self.admin_site)
class VideoPlayerForm(BasePlayerForm): file = PlusModelChoiceField(label=_("Video File"), queryset=FilerFileModel.objects.all(), widget=AdminFileWidget( ManyToOneRel(FilerFileField, FilerFileModel, 'id'), admin_site), required=True, validators=[validate_video_file], help_text=_("Video file for the player")) poster = PlusModelChoiceField( label=_("Video Thumbnail"), queryset=FilerFileModel.objects.all(), widget=AdminFileWidget( ManyToOneRel(FilerFileField, FilerFileModel, 'id'), admin_site), required=False, help_text=_("Thumbnail on stopped video file "), )
class AudioPlayerForm(BasePlayerForm): file = PlusModelChoiceField( label=_("Audio File"), queryset=FilerFileModel.objects.all(), widget=AdminFileWidget( ManyToOneRel(FilerFileField, FilerFileModel, 'id'), admin_site), required=True, validators=[validate_audio_file], help_text=_("Audio file for the player"), )
def get_form(self, request, obj=None, **kwargs): if 'fields' in kwargs: fields = kwargs.pop('fields') else: fields = flatten_fieldsets(self.get_fieldsets(request, obj)) excluded = self.get_exclude(request, obj) exclude = [] if excluded is None else list(excluded) readonly_fields = self.get_readonly_fields(request, obj) exclude.extend(readonly_fields) if excluded is None and hasattr(self.form, '_meta') and self.form._meta.exclude: # Take the custom ModelForm's Meta.exclude into account only if the # ModelAdmin doesn't define its own. exclude.extend(self.form._meta.exclude) # if exclude is an empty list we pass None to be consistent with the # default on modelform_factory exclude = exclude or None new_attrs = OrderedDict( (f, None) for f in readonly_fields if f in self.form.declared_fields ) form = type(self.form.__name__, (self.form,), new_attrs) try: model = obj.content_type.model_class() model_key = model._meta.pk.name except (AttributeError, ObjectDoesNotExist): model = self.model.content_type.field.formfield().choices.queryset[0].model_class() model_key = 'id' defaults = { "form": form, "fields": fields, "exclude": exclude, "formfield_callback": partial(self.formfield_for_dbfield, request=request), "widgets": { 'content_type': ContentTypeSelect, 'object_id': ForeignKeyRawIdWidget( rel=ManyToOneRel(model_key, model, 'id'), admin_site=admin.site ) } } defaults.update(kwargs) if defaults['fields'] is None and not modelform_defines_fields(defaults['form']): defaults['fields'] = forms.ALL_FIELDS try: return modelform_factory(self.model, **defaults) except FieldError as e: raise FieldError( '%s. Check fields/fieldsets/exclude attributes of class %s.' % (e, self.__class__.__name__) )
def __init__(self, *args, **kwargs): super(AdvertModelForm, self).__init__(*args, **kwargs) try: model = self.instance.content_type.model_class() model_key = model._meta.pk.name except (AttributeError, ObjectDoesNotExist): model = self.fields['content_type'].queryset[0].model_class() model_key = 'id' self.fields['object_id'].widget = ForeignKeyRawIdWidget( rel=ManyToOneRel(model_key, model, 'id'), admin_site=admin.site )
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if self.instance.pk and self.instance.cinema_hall: cinema = self.instance.cinema_hall.cinema self.fields['cinema_hall'].queryset = CinemaHall.objects.filter( cinema=cinema) self.fields['film'].queryset = Film.objects.filter( agreements__in=cinema.get_agreements().filter_by_date( self.instance.date)).distinct() else: rel = ManyToOneRel(models.models.ForeignKey, models.CinemaHall, 'id') self.fields['cinema_hall'].widget = widgets.ForeignKeyRawIdWidget( rel, site)
def __init__(self, *args, **kwargs): super(RoleForm, self).__init__(*args, **kwargs) if self.instance.pk is None: unassigned_users = models.Role.objects.filter(user_id__isnull=False).values_list('user_id', flat=True) self.fields['user'].queryset = User.objects.exclude(id__in=unassigned_users) # noinspection PyProtectedMember relation = ManyToOneRel(User._meta.get_field('id'), User, 'id') self.fields['user'].widget = RelatedFieldWidgetWrapper(self.fields['user'].widget, relation, admin.site) self.fields['user_selected'].widget = forms.HiddenInput() else: self.fields['user'].widget = forms.HiddenInput() self.fields['user_selected'].initial = self.instance.user.username self.fields['first_name'].initial = self.instance.user.first_name self.fields['last_name'].initial = self.instance.user.last_name self.fields['email'].initial = self.instance.user.email self.fields['is_active'].initial = self.instance.user.is_active
def __init__(self, *args, **kwargs): super(MetatypeAdminForm, self).__init__(*args, **kwargs) rel = ManyToOneRel(Metatype, 'id') self.fields['parent'].widget = RelatedFieldWidgetWrapper( self.fields['parent'].widget, rel, self.admin_site)
def __init__(self, *args, **kwargs): super(CategoryAdminForm, self).__init__(*args, **kwargs) rel = ManyToOneRel(Category._meta.get_field('tree_id'), Category, 'id') self.fields['parent'].widget = RelatedFieldWidgetWrapper( self.fields['parent'].widget, rel, self.admin_site)