AUTH_USER_MODEL refers to model .. that has not been installed and created AbstractUser models not able to login
AUTH_USER_MODEL error solved in EDIT3. Passwords still will not save on user creation via form.
I'm using Django 1.5 playing around with the new user override/extension features, and I am not able to register new users via my registration form - only via the Admin. When registering via the registration form, I get the following error:
Manager isn't available; User has been swapped for 'poker.PokerUser'
models.py:
class PokerUser(AbstractUser): poker_relate = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True) token = models.EmailField() USER_CHOICES = ( ('1', 'Staker'), ('2', 'Horse') ) user_type = models.CharField(choices=USER_CHOICES, max_length=10) username1 = models.CharField(null=True, blank=True, max_length=40) username2 = models.CharField(null=True, blank=True, max_length=40) username3 = models.CharField(null=True, blank=True, max_length=40) username4 = models.CharField(null=True, blank=True, max_length=40) username5 = models.CharField(null=True, blank=True, max_length=40)
PokerUserForm model:
class PokerUserForm(UserCreationForm): class Meta: model = PokerUser fields = ('username','password1','password2','email','user_type','token','username1','username2','username3','username4','username5',)
I've attempted to change the model in the PokerUserForm model to use get_user_model() instead of explicitly defining the model by setting model = get_user_model() instead of model = PokerUser but then I receive the following error:
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'poker.PokerUser' that has not been installed
My AUTH_USER_MODEL is setup in my settings.py like so:
AUTH_USER_MODEL = 'poker.PokerUser'
On we go - my Registration view in views.py:
def UserRegistration(request): player = PokerUser() if request.method == 'POST': form = PokerUserForm(request.POST, instance=player) if form.is_valid(): player.email_address = form.cleaned_data['email'] player.user_type = form.cleaned_data['user_type'] # if player is staker, token is their own email. otherwise their token is their staker's email and # their relation is their staker if player.user_type == '1' or player.user_type == 'staker': player.token = player.email_address else: player.token = form.cleaned_data['token'] staker = PokerUser.objects.get(email=player.token) player.poker_relate = staker player.save() return HttpResponseRedirect('/') else: form = PokerUserForm() initialData = {'form': form} csrfContext = RequestContext(request, initialData) return render_to_response('registration/register.html', csrfContext)
EDIT1:
According to the docs, the UserCreationForm must be recreated for use with custom user classes.
I overrode the entire UserCreationForm as follows:
class UserCreationForm(forms.ModelForm): """ A form that creates a user, with no privileges, from the given username and password. """ error_messages = { 'duplicate_username': _("A user with that username already exists."), 'password_mismatch': _("The two password fields didn't match."), } username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.@+-]+$', help_text=_("Required. 30 characters or fewer. Letters, digits and " "@/./+/-/_ only."), error_messages={ 'invalid': _("This value may contain only letters, numbers and " "@/./+/-/_ characters.")}) password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput) password2 = forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput, help_text=_("Enter the same password as above, for verification.")) class Meta: model = PokerUser fields = ('username','password1','password2','email','user_type','token','username1','username2','username3','username4','username5',) def clean_username(self): # Since User.username is unique, this check is redundant, # but it sets a nicer error message than the ORM. See #13147. username = self.cleaned_data["username"] try: PokerUser.objects.get(username=username) except PokerUser.DoesNotExist: return username raise forms.ValidationError(self.error_messages['duplicate_username']) def clean_password2(self): password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: raise forms.ValidationError( self.error_messages['password_mismatch']) return password2 def save(self, commit=True): user = super(UserCreationForm, self).save(commit=False) user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user
And this was able to resolve this error:
The Manager isn't available; User has been swapped for 'poker.PokerUser'
Now, the users get created but are not able to log in. When I check the users in the admin, all of the information seems to be correct except for the password. Adding a password manually in the admin does not seem to work correctly. Still, adding users via the admin work correctly.
EDIT 2:
I'm still unable to login as any of my AbstractUser models created via the registration form. I have completely overridden the UserCreationForm as outlined above, and am unable to implement get_user_model() with this error:
AUTH_USER_MODEL refers to model 'poker.PokerUser' that has not been installed
The Django code for get_user_model() is:
def get_user_model(): "Return the User model that is active in this project" from django.conf import settings from django.db.models import get_model try: app_label, model_name = settings.AUTH_USER_MODEL.split('.') except ValueError: raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'") user_model = get_model(app_label, model_name) if user_model is None: raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL) return user_model
Since I have AUTH_USER_MODEL = 'poker.PokerUser' setup in my settings.py, this should work. I've verified this through the Django console:
>>> from django.contrib.auth import get_user_model>>> settings.AUTH_USER_MODELOut[14]: 'poker.PokerUser'>>> from django.db.models import get_model>>> app_label, model_name = settings.AUTH_USER_MODEL.split('.')>>> user_model = get_model(app_label, model_name)>>> user_modelOut[18]: poker.models.PokerUser
However the implementation still does not work correctly.
If you've read this far, thanks!
EDIT3:
AUTH_USER_MODEL refers to model 'poker.PokerUser' that has not been installed has been fixed. I accidentally had the UserCreationForm that I recreated in poker.models instead of registration.forms, so when I ran get_user_model() that was assigned to poker.PokerUser, it couldn't resolve since it was already in that location.
Now the only issue left is that when creating new users, their passwords will not save. I've narrowed it down to a single method in the UserCreationForm by placing print statements here:
def clean_password2(self): password1 = self.cleaned_data.get("password1") print password1 password2 = self.cleaned_data.get("password2") print password2 if password1 and password2 and password1 != password2: raise forms.ValidationError( self.error_messages['password_mismatch']) print password2 return password2def save(self, commit=True): user = super(UserCreationForm, self).save(commit=False) user.set_password(self.cleaned_data["password1"]) print self.cleaned_data["password1"] if commit: user.save() return user
The print password1 and print password1 statements in clean_password2 display the plain text password, but print self.cleaned_data["password1"] in the save method is blank. Why is my form data not being passed to the save method?
TL;DR AbstractUser model creation is working in both Admin and via registration form, but only the users created via Admin are able to login. The users created via the registration form are unable to log in and seem to be saved without a password - all other information is saved correctly.
https://codehunter.cc/a/django/auth-user-model-refers-to-model-that-has-not-been-installed-and-created-abstractuser-models-not-able-to-login
0 notes
django ModelForm
>>> from django.forms import ModelForm >>> from myapp.models import Article # Create the form class. >>> class ArticleForm(ModelForm): ... class Meta: ... model = Article ... fields = ['pub_date', 'headline', 'content', 'reporter'] # Creating a form to add an article. >>> form = ArticleForm() # Creating a form to change an existing article. >>> article = Article.objects.get(pk=1) >>> form = ArticleForm(instance=article)
ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet.
ManyToManyField is represented by django.forms.ModelMultipleChoiceField, which is a MultipleChoiceField whose choices are a model QuerySet.
model 에서 blank=True 설정 되어있으면 modelform에서는 required 값이 False되고 그외에는 required=True.
form 필드 label 는 model field의 verbose_name 의 값을 이용하되 첫글자는 대문자로 한다.
form 필드의 help_text 는 model의 help_text을 이용한다.
model field 가 만약 choices set 이면 form field의 widget은 Select가 된다.보통은 blank 가 선택되어있는데 blank=False가 설정되어있다면 blank는 없어진다. 만약 model field 가 default값이 설정되어 있다면 그것이 initially selected 가 된다.
modelform은 기본적으로 model의 field를 따라서 만들어지게 되는데 이를 override하고 싶다면 Overriding the default fields 를 따른다.
Validation on a ModelForm
There are two main steps involved in validating a ModelForm:
Validating the form
Validating the model instance
is_valid()를 호출하거나, errors에 접근하거나 full_clean()를 직접호출하면 validation 과정이 시작된다.
form validation을 원래대로 수행하고 model validation을 수행한다.
Overriding the clean() method
You can override the clean() method on a model form to provide additional validation in the same way you can on a normal form.
Interaction with model validation
As part of the validation process, ModelForm will call the clean() method of each field on your model that has a corresponding field on your form. If you have excluded any model fields, validation will not be run on those fields.
model validation에서 full_clean()을 호출하면 차례로 clean_fields() , clean(), validate_unique()가 호출되면서 validatoin을 수행하게 된다. Model.full_clean(exclude=None, validate_unique=True) 에서 특정 fields를 validation에서 제외할수 있다. 또 Model.clean_fields(exclude=None)에서도 선택적으로 제외할수 있다. 이렇게 제외된 것은 modelform validation에서도 제외된다는 이야기 이다.
See the form validation documentation for more on how field cleaning and validation work.
The model’s clean() method will be called before any uniqueness checks are made. See Validating objects for more information on the model’s clean() hook.
Considerations regarding model’s error_messages
form field level에서 정의 된 Error messages 나 form Meta level 에서 정의된 Error messages가 항상 model field level에서 정의된 것보다 우선한다. 다만 form level에서 error가 발생하지 않고 model validation에서 error가 발생한경우만 model field level의 error messages를 사용한다.
NON_FIELD_ERRORS 에 model validation에서 발생한 error_messages 를 dictionary 형태로 Meta class에 정의해 넣을수 있다.
from django.core.exceptions import NON_FIELD_ERRORS from django.forms import ModelForm class ArticleForm(ModelForm): class Meta: error_messages = { NON_FIELD_ERRORS: { 'unique_together': "%(model_name)s's %(field_labels)s are not unique.", } }
The save()method
ModelForm 는 save() method를 가지며 이미 존재하는 model instance를 instance키워드 argument로 전달 받으면 update를 수행하고 아니면 create을 수행한다.
>>> from myapp.models import Article >>> from myapp.forms import ArticleForm # Create a form instance from POST data. >>> f = ArticleForm(request.POST) # Save a new Article object from the form's data. >>> new_article = f.save() # Create a form to edit an existing Article, but use # POST data to populate the form. >>> a = Article.objects.get(pk=1) >>> f = ArticleForm(request.POST, instance=a) >>> f.save()
validation을 수행하지 않고 바로 save()를 수행하는 경우 form.errors를 save()과정에서 확인 하게 되는데 확인하면 바로 validation이 수행된다. 그래서 문제 있는 경우 form.errors는 True값으로 표현되며 ValueError가 raise된다.
꼭 입력되지 않아도 되는 model field에 어떤 값이 주어지지 않으면 model field의 default값이 자동으로 채워지게 된다. 단 이 작동방식은 CheckboxInput, CheckboxSelectMultiple, or SelectMultiple (or any custom widget whose value_omitted_from_data() method always returns False) 에는 적용되지 않는다.
This save() method accepts an optional commit keyword argument, which accepts either True or False. If you call save() with commit=False, then it will return an object that hasn’t yet been saved to the database. 여기서 database에 저장되지 않은 상태에서 개발자가 추가로 data를 임의로 넣을수있다.
commit=False를 사용한 경우 model에 many-to-many relation field가 있다면 Django 가 save_m2m() method 를 ModelForm class에 제공하는데 아래와 같이 instance 를 가진 form을 먼저 저장하고 save_m2m() 를 호출해주어야 한다.
# Create a form instance with POST data. >>> f = AuthorForm(request.POST) # Create, but don't save the new author instance. >>> new_author = f.save(commit=False) # Modify the author in some way. >>> new_author.some_field = 'some_value' # Save the new instance. >>> new_author.save() # Now, save the many-to-many data for the form. >>> f.save_m2m()
Calling save_m2m() is only required if you use save(commit=False). When you use a save() on a form, all data – including many-to-many data – is saved without the need for any additional method calls. For example:
# Create a form instance with POST data. >>> a = Author() >>> f = AuthorForm(request.POST, instance=a) # Create and save the new author instance. There's no need to do anything else. >>> new_author = f.save()
is_multipart() method is used to determine whether a form requires multipart file upload (and hence whether request.FILES must be passed to the form), etc. See Binding uploaded files to a form for more information.
Selecting the fields to use
Meta에 fields를 설정해서 어떤 fields가 form에 포함될지를 결정한다.
'__all__' 모든 fields사용을 나타낸다.
from django.forms import ModelForm class AuthorForm(ModelForm): class Meta: model = Author fields = '__all__'
exclude 는 몇몇을 제외한다.
class PartialAuthorForm(ModelForm): class Meta: model = Author exclude = ['title']
model 에 정의되어 있는 field의 순대로 form에 위치하게 된다. 단 ManyToManyField 는 마지막에 위치한다.
model field에 editable=False 설정했으며 form에는 포함되지 않는다.
Note
form Meta fields에 포함되어있지 않거나 exclude를 통해 제외된 field를 model단계에서저장할때 값을 요구하는 경우가 있다. 이런경우는 개발자가 손수 추가로 해당 data를 넣어 주어야 한다.
author = Author(title='Mr') form = PartialAuthorForm(request.POST, instance=author) form.save()
Alternatively, you can use save(commit=False) and manually set any extra required fields:
form = PartialAuthorForm(request.POST) author = form.save(commit=False) author.title = 'Mr' author.save()
Overriding the default fields
기본적으로 model에 정의되어있는 field에 따라 modelform field도 결정된다. 다만 Meta에 widgets이 설정하면 설정에 따라 바뀌게 된다.
from django.forms import ModelForm, Textarea from myapp.models import Author class AuthorForm(ModelForm): class Meta: model = Author fields = ('name', 'title', 'birth_date') widgets = { 'name': Textarea(attrs={'cols': 80, 'rows': 20}), }
widgets dictionary 에는 widget instances (e.g., Textarea(...)) 나 classes (e.g., Textarea)를 넣을수 있다. non-empty choices attribute 성질을 가지는 field의 경우에는 widgets dictionary 를 이용해서 바꿀수 없고 Meta밖에 명시적으로 field성질을 명시 해줘야 한다.
widgets을 이용해서 그 형태를 바꾼것과 같은 방법으로 labels, help_texts, error_messages 도 Meta class에서 바꿀수 있다.
from django.utils.translation import gettext_lazy as _ class AuthorForm(ModelForm): class Meta: model = Author fields = ('name', 'title', 'birth_date') labels = { 'name': _('Writer'), } help_texts = { 'name': _('Some useful help text.'), } error_messages = { 'name': { 'max_length': _("This writer's name is too long."), }, }
field_classes 를 통해 custom field를 사용할수 있다.
예시)
from django.forms import ModelForm from myapp.models import Article class ArticleForm(ModelForm): class Meta: model = Article fields = ['pub_date', 'headline', 'content', 'reporter', 'slug'] field_classes = { 'slug': MySlugFormField, }
field를 좀더 customize해서 사용하고싶은 경우 예를 들어 its type, validators, required 등을 바꾸고 싶은 경우 일반 Form에서 field를 작성하듯 명시적으로 field를 작성해 주면 된다.
from django.forms import CharField, ModelForm from myapp.models import Article class ArticleForm(ModelForm): slug = CharField(validators=[validate_slug]) class Meta: model = Article fields = ['pub_date', 'headline', 'content', 'reporter', 'slug']
Note
ModelForm is a regular Form which can automatically generate certain fields. The fields that are automatically generated depend on the content of the Meta class and on which fields have already been defined declaratively. Basically, ModelForm will only generate fields that are missing from the form, or in other words, fields that weren’t defined declaratively.
아래와 같이 이해했다.
ModelForm 은 Meta class 에 정의된 fields 내용과 ModelForm에 명시적으로 표기된 fields 내용을 기반으로 form을 만든다. Meta fields에 기록된 fields중에 ModelForm에 명시적으로 표기된것이 아닌것은 model을 기반으로 자동으로 만든다.
field에 대한 속성이 Meta에도 있고 ModelForm에도 명시적으로 속성이 정의된경우(일반 Form에서 field 속성 정의하듯) 명시적으로 정의된것이 우선한다. 또 명시적으로 속성을 정의한 경우 model에 정의된 속성이 무시되므로 다시 정의해주어야 한다. 예를 들어 max_length가 model에 정의되어 있고 해당 field를 명시적으로 override한 경우 ModelForm에서도 정의해주어야 한다.
class Article(models.Model): headline = models.CharField( max_length=200, null=True, blank=True, help_text='Use puns liberally', ) content = models.TextField()
class ArticleForm(ModelForm): headline = MyFormField( max_length=200, required=False, help_text='Use puns liberally', ) class Meta: model = Article fields = ['headline', 'content']
ModeForm에서 Meta class를 이용하건 Form에서 명시적으로 override하건 두경우다 field가 기존 data를 받아들일수 있는 형식이어야 한다. 아닌 경우
ValueError 가 raise된다.
Enabling localization of fields
By default, the fields in a ModelForm will not localize their data. To enable localization for fields, you can use the localized_fields attribute on the Meta class.
>>> from django.forms import ModelForm >>> from myapp.models import Author >>> class AuthorForm(ModelForm): ... class Meta: ... model = Author ... localized_fields = ('birth_date',)
If localized_fields is set to the special value '__all__', all fields will be localized.
Form inheritance
extra fields 또는 extra methods 를 추가 하고자 하는 경우 inheritance 하는 것도 좋은 방법이다.
>>> class EnhancedArticleForm(ArticleForm): ... def clean_pub_date(self):
위의 경우 pub_date field 에 대한 extra validation 를 추가하는 것을 보여준다.
ModelForm을 inherit하는 경우도 있지만 아래와 같이 ModelForm안의 Meta class를 inherit할수도 있다.
>>> class RestrictedArticleForm(EnhancedArticleForm): ... class Meta(ArticleForm.Meta): ... exclude = ('body',)
This adds the extra method from the EnhancedArticleForm and modifies the original ArticleForm.Meta to remove one field.
There are a couple of things to note, however.
Normal Python name resolution rules apply. If you have multiple base classes that declare a Meta inner class, only the first one will be used. This means the child’s Meta, if it exists, otherwise the Meta of the first parent, etc.
Form 이나 ModelForm 를 inherit할수 있고 둘가지를 동시에 할수도 있는데 둘다 하는 경우 ModelForm MRO에서 맨우선할수 있도록 inherit class을 기입할때 가장 먼저 한다.
parent class에 명시적으로 정의된 field의 경우 child class에서 field이름= None을 통해 하부 클래스에서 없앨수 있다. ModelForm metaclass 를 통해 생성된 fields는 이 방법으로 제거할수 없다.그런경우 이방법 Selecting the fields to use. 을 사용한다.
Providing initial values
일반 form과 마찬가지로 form 생성될때 initial parameter 를 이용 Initial values를 가지게 할수 있다. 이때 model field, form field 에서 정의된 initial값은 무시된다.
>>> article = Article.objects.get(pk=1) >>> article.headline 'My headline' >>> form = ArticleForm(initial={'headline': 'Initial headline'}, instance=article) >>> form['headline'].value() 'Initial headline'
ModelForm factory function
>>> from django.forms import modelform_factory >>> from myapp.models import Book >>> BookForm = modelform_factory(Book, fields=("author", "title"))
어떤 fields들이 들어갈지 위와 같이 지정할수 있다. (fields 와 exclude를 사용할수 있다)
>>> from django.forms import Textarea >>> Form = modelform_factory(Book, form=BookForm, ... widgets={"title": Textarea()})
아래와 같이 localization 을 설정할수 있다.
>>> Form = modelform_factory(Author, form=AuthorForm, localized_fields=("birth_date",))
Model formsets¶
class models.BaseModelFormSet
>>> from django.forms import modelformset_factory >>> from myapp.models import Author >>> AuthorFormSet = modelformset_factory(Author, fields=('name', 'title'))
fields 와 exclude를 이용 어떤 form에 어떤 fields가 나오게 할지 지정할수 있다.
>>> AuthorFormSet = modelformset_factory(Author, exclude=('birth_date',))
>>> formset = AuthorFormSet() >>> print(formset) <input type="hidden" name="form-TOTAL_FORMS" value="1" id="id_form-TOTAL_FORMS"><input type="hidden" name="form-INITIAL_FORMS" value="0" id="id_form-INITIAL_FORMS"><input type="hidden" name="form-MAX_NUM_FORMS" id="id_form-MAX_NUM_FORMS"> <tr><th><label for="id_form-0-name">Name:</label></th><td><input id="id_form-0-name" type="text" name="form-0-name" maxlength="100"></td></tr> <tr><th><label for="id_form-0-title">Title:</label></th><td><select name="form-0-title" id="id_form-0-title"> <option value="" selected>---------</option> <option value="MR">Mr.</option> <option value="MRS">Mrs.</option> <option value="MS">Ms.</option> </select><input type="hidden" name="form-0-id" id="id_form-0-id"></td></tr>
Note
When using multi-table inheritance, forms generated by a formset factory will contain a parent link field (by default <parent_model_name>_ptr) instead of an id field. (잘 이해가 되지 않음)
Changing the queryset
기본적으로 Modelformset의 경우 model의 모든 obj들 가지는 queryset을 사용 form들을 만들게 된다. 이를 바꾸기 위해 queryset argument를 아래와 같이 사용한다.
>>> formset = AuthorFormSet(queryset=Author.objects.filter(name__startswith='O'))
또 다른 방법으로는 queryset property값을 아래와 같이 지정해 주는 것이다.
from django.forms import BaseModelFormSet from myapp.models import Author class BaseAuthorFormSet(BaseModelFormSet): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.queryset = Author.objects.filter(name__startswith='O')
Then, pass your BaseAuthorFormSet class to the factory function:
>>> AuthorFormSet = modelformset_factory( ... Author, fields=('name', 'title'), formset=BaseAuthorFormSet)
기본 instance를 이용하지 않으려는 경우 아래 방법을 사용한다.
>>> AuthorFormSet(queryset=Author.objects.none())
Changing the form
modelformset_factory 는 기본적으로 modelform_factory()를 이용해 form을 만든다.그러나 아래와 같이 form을 만들어 사용할수도 있다. 아래의 경우 validation과정을 바꾼경우 이다.
class AuthorForm(forms.ModelForm): class Meta: model = Author fields = ('name', 'title') def clean_name(self): # custom validation for the name field ...
아래와 model form 을 만들어서 factory에 전달한다.
AuthorFormSet = modelformset_factory(Author, form=AuthorForm)
꼭 위에와 같이 model form을 만들어서 사용할 필요는 없다.
modelformset_factory function의 몇몇 arguments 설정으로 통해 form을 조정할수 있기 때문이다. modelformset_factory 설정 arguments들은 modelform_factory를 거쳐 만들어질 form을 변경하게 된다.
modelformset_factory function의 arguments 설정으로 통해 form을 조정하는 방법
Specifying widgets to use in the form with widgets
Using the widgets parameter. This works the same way as the widgets dictionary on the inner Meta class of a ModelForm works:
>>> AuthorFormSet = modelformset_factory( ... Author, fields=('name', 'title'), ... widgets={'name': Textarea(attrs={'cols': 80, 'rows': 20})})
Enabling localization for fields with localized_fields
Using the localized_fields parameter, you can enable localization for fields in the form.
>>> AuthorFormSet = modelformset_factory( ... Author, fields=('name', 'title', 'birth_date'), ... localized_fields=('birth_date',))
If localized_fields is set to the special value '__all__', all fields will be localized.
Providing initial values
regular formsets과 마찬가지로 formset class를 만들때 사용하는 modelformset_factory()에 initial parameter를 이용해서 initial data 를 설정할수 있다. 다만 initial values 들은 이미 존재하는 model obj에 적용되는 것이 아니라 extra로 만들어지는 form에만 적용된다. extra에 initialized된 값들이 변경되지 않으면 사용자가 변경하지 않은 것으로 알고 해당 form은 validation, save를 거치지 않게 된다.
Saving objects in the formset
ModelForm과 마찬가지로 save()를 통해 저장한다. save()호출하면 저절로 validation작업을수행하게 된다.
# Create a formset instance with POST data. >>> formset = AuthorFormSet(request.POST) # Assuming all is valid, save the data. >>> instances = formset.save()
save() 는 save작업을 하며 저장된 instances를 리턴한다. 변화되지 않는 form은 validation, save작업도 하지 않으며 결과 instances에도 포함되지 않는다.
model에는 존재하나 ModelForm에는 fields, exclude 등의 방법으로 포함되지 않은 경우 값은 단순히 save()작업으로 값이 입력되지는 않는다. 입력이 필요하다면 개발자가 직접 입력하는 작업을 수행해야 한다.
commit=False 를 이용 unsaved model instances 를 얻은 다음 data를 추가하는 작업을 아래와 같이 할수 있다.
# don't save to the database >>> instances = formset.save(commit=False) >>> for instance in instances: ... # do something with instance ... instance.save()
If your formset contains a ManyToManyField, you’ll also need to call formset.save_m2m() to ensure the many-to-many relationships are saved properly.
save() 호출후에 formset obj는 아래 속성을 추가로 갖게 된다.
models.BaseModelFormSet.changed_objects
models.BaseModelFormSet.deleted_objects
models.BaseModelFormSet.new_objects
Limiting the number of editable objects
일반 formsets과 마찬가지로 max_num 과 extra parameters 를 사용할수 있다. 이를 통해 modelformset_factory()를 통해 extra forms 갯수를 조정할수 있다.
max_num 는 기존 model obj를 위한 form을 막지는 않는다.
>>> Author.objects.order_by('name') <QuerySet [<Author: Charles Baudelaire>, <Author: Paul Verlaine>, <Author: Walt Whitman>]> >>> AuthorFormSet = modelformset_factory(Author, fields=('name',), max_num=1) >>> formset = AuthorFormSet(queryset=Author.objects.order_by('name')) >>> [x.name for x in formset.get_queryset()] ['Charles Baudelaire', 'Paul Verlaine', 'Walt Whitman']
extra=0 라 하더라도 form이 생성되는 것을 막는 것은 아니다. 예를 들어 JavaScript 를 이용 새 form을 추가 할수 있다. 또한 추가된 form의 POST data를 막는 것도 아니다. 즉 extra=0라고 하더라도 create작업을 수행할수도 있다. 다만 formset이 extra form을 생성해 주는 것이 아닌 것이다.
If the value of max_num is greater than the number of existing related objects, up to extra additional blank forms will be added to the formset, so long as the total number of forms does not exceed max_num:
>>> AuthorFormSet = modelformset_factory(Author, fields=('name',), max_num=4, extra=2) >>> formset = AuthorFormSet(queryset=Author.objects.order_by('name')) >>> for form in formset: ... print(form.as_table()) <tr><th><label for="id_form-0-name">Name:</label></th><td><input id="id_form-0-name" type="text" name="form-0-name" value="Charles Baudelaire" maxlength="100"><input type="hidden" name="form-0-id" value="1" id="id_form-0-id"></td></tr> <tr><th><label for="id_form-1-name">Name:</label></th><td><input id="id_form-1-name" type="text" name="form-1-name" value="Paul Verlaine" maxlength="100"><input type="hidden" name="form-1-id" value="3" id="id_form-1-id"></td></tr> <tr><th><label for="id_form-2-name">Name:</label></th><td><input id="id_form-2-name" type="text" name="form-2-name" value="Walt Whitman" maxlength="100"><input type="hidden" name="form-2-id" value="2" id="id_form-2-id"></td></tr> <tr><th><label for="id_form-3-name">Name:</label></th><td><input id="id_form-3-name" type="text" name="form-3-name" maxlength="100"><input type="hidden" name="form-3-id" id="id_form-3-id"></td></tr>
A max_num value of None (the default) puts a high limit on the number of forms displayed (1000). In practice this is equivalent to no limit.
Using a model formset in a view
from django.forms import modelformset_factory from django.shortcuts import render from myapp.models import Author def manage_authors(request): AuthorFormSet = modelformset_factory(Author, fields=('name', 'title')) if request.method == 'POST': formset = AuthorFormSet(request.POST, request.FILES) if formset.is_valid(): formset.save() # do something. else: formset = AuthorFormSet() return render(request, 'manage_authors.html', {'formset': formset})
As you can see, the view logic of a model formset isn’t drastically different than that of a “normal” formset. The only difference is that we call formset.save() to save the data into the database. (This was described above, in Saving objects in the formset.)
Overriding clean() on a ModelFormSet
Just like with ModelForms, by default the clean() method of a ModelFormSet will validate that none of the items in the formset violate the unique constraints on your model (either unique, unique_together or unique_for_date|month|year). If you want to override the clean() method on a ModelFormSet and maintain this validation, you must call the parent class’s clean method:
from django.forms import BaseModelFormSet class MyModelFormSet(BaseModelFormSet): def clean(self): super().clean() # example custom validation across forms in the formset for form in self.forms: # your custom formset validation ...
Also note that by the time you reach this step, individual model instances have already been created for each Form. Modifying a value in form.cleaned_data is not sufficient to affect the saved value. If you wish to modify a value in ModelFormSet.clean() you must modify form.instance:
from django.forms import BaseModelFormSet class MyModelFormSet(BaseModelFormSet): def clean(self): super().clean() for form in self.forms: name = form.cleaned_data['name'].upper() form.cleaned_data['name'] = name # update the instance value. form.instance.name = name
Using a custom queryset
As stated earlier, you can override the default queryset used by the model formset:
from django.forms import modelformset_factory from django.shortcuts import render from myapp.models import Author def manage_authors(request): AuthorFormSet = modelformset_factory(Author, fields=('name', 'title')) if request.method == "POST": formset = AuthorFormSet( request.POST, request.FILES, queryset=Author.objects.filter(name__startswith='O'), ) if formset.is_valid(): formset.save() # Do something. else: formset = AuthorFormSet(queryset=Author.objects.filter(name__startswith='O')) return render(request, 'manage_authors.html', {'formset': formset})
Note that we pass the queryset argument in both the POST and GET cases in this example.
Using the formset in the template
formset을 template에 render하는 방법3가지
1.
<form method="post"> {{ formset }} </form>
2.
<form method="post"> {{ formset.management_form }} {% for form in formset %} {{ form }} {% endfor %} </form>
When you manually render the forms yourself, be sure to render the management form as shown above. See the management form documentation.
3.
<form method="post"> {{ formset.management_form }} {% for form in formset %} {% for field in form %} {{ field.label_tag }} {{ field }} {% endfor %} {% endfor %} </form>
위 방법은 form.id를 수동으로 만들지 않는 점 유의
아래방법은 form.id가 있어야 된다.
<form method="post"> {{ formset.management_form }} {% for form in formset %} {{ form.id }} <ul> <li>{{ form.name }}</li> <li>{{ form.age }}</li> </ul> {% endfor %} </form>
need to explicitly render {{ form.id }}
0 notes