class BookForm(ModelForm): class Meta: model = Book fields = ['name', 'author', 'mentions_persons'] name = fields.AutoCompleteField('name') author = fields.AutoCompleteSelectField('author') mentions_persons = fields.AutoCompleteSelectMultipleField('person')
class BasketDetailForm(forms.ModelForm): """ Форма выбора полей продукта и юзера в модели корзины """ class Meta: model = Basket exclude = ('date_hierarchy', ) user = fields.AutoCompleteSelectField('users', label='Клиент') product = fields.AutoCompleteSelectMultipleField( 'products', label='Товар', help_text="""Введите имя товара.""")
class UserOrderForm(forms.ModelForm): """ Форма выбора поля user в модели Order """ class Meta: model = Order exclude = ('', ) user = fields.AutoCompleteSelectField('users', label='Клиент') product = fields.AutoCompleteSelectMultipleField( 'products', label='Товар', help_text="""Введите имя товара.""") quantity = forms.IntegerField(label='Кол-во товара') def __init__(self, *args, **kwargs): """dander __init__ redefined for set initial param for attribute quantity""" super().__init__(*args, **kwargs) instance: Order = kwargs.get('instance') if isinstance(instance, self._meta.model): self.initial['quantity'] = OrderDetails.objects.filter( order_id=instance.pk).only( 'quantity').order_by().first().quantity
def test_has_changed_blank_input(self): field = fields.AutoCompleteSelectMultipleField('book') self.assertTrue(field.has_changed(None, ['1'])) self.assertFalse(field.has_changed(None, []))
def test_has_changed(self): field = fields.AutoCompleteSelectMultipleField('book') self.assertFalse(field.has_changed([1], ['1'])) self.assertFalse(field.has_changed(['abc'], ['abc'])) self.assertTrue(field.has_changed([1], ['2']))