I have a model named Project containing three ManyToManyFields (member_student, supervisor and tag). One (tag) of which is excluded in the form and it has to be saved manually. So, in the view, I use save(commit=False) because I have to change some fields of the form. After changing the fields, the form is saved and I add the tags one by one. Then, when I call save_m2m to save ManyToManyField, I get the error given by the save_m2m line in views:
invalid literal for int() with base 10: 'a'
Here is my model.
class Tag(models.Model):
name = models.CharField(unique=True, max_length=60)
slug = models.SlugField(max_length=60, unique=True)
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.name)
super(Tag, self).save(*args, **kwargs)
class Project(models.Model):
'''Main Project uploading'''
title = models.CharField(max_length=300)
description = models.TextField(null=True)
#year = models.ForeignKey(Year)
tag = models.ManyToManyField(Tag)
owner_student = models.ForeignKey(Student, related_name='member_student')
member_student = models.ManyToManyField(Student, blank=True, null=True)
supervisor = models.ManyToManyField(Supervisor, blank=True, null=True)
subject = models.ForeignKey(Subject)
main_document = models.FileField(upload_to='main_documents/')
supporting_document = models.FileField(upload_to='supp_documents/', blank=True, null=True)
source_code = models.FileField(upload_to='source_code/', blank=True, null=True)
screenshot = models.ImageField(upload_to='screenshots/', blank=True, null=True)
This is the forms.py:
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
exclude = ['owner_student', 'slug', 'tag']
tag = forms.CharField(max_length=100)
Here is the View.
def add_project(request):
parameters = {}
if request.method =="POST":
upload_form = ProjectForm(request.POST, request.FILES)
if upload_form.is_valid():
new_form = upload_form.save(commit=False)
mystud = Student.objects.get(user=request.user)
new_form.owner_student = mystud
new_form.save()
tags = upload_form.cleaned_data['tag']
tags = tags.split(',')
for eachtag in tags:
tag, created = Tag.objects.get_or_create(name=eachtag.strip())
tag.save()
new_form.tag.add(tag)
upload_form.save_m2m()
return HttpResponseRedirect(reverse(project_page, args=(new_form.slug,)))
else:
parameters["upload_form"] = upload_form
return render_to_response('upload.html', parameters)
else:
upload_form = ProjectForm()
parameters["upload_form"] = upload_form
parameters["page_title"] = "Upload your Project"
return render_to_response('upload.html', parameters)
So, my question is how can I save the tags as well as the two other ManyToManyField without getting error ? I guess the save_m2m function is giving error because of the tuple returned by get_or_create.
Don't use 'tag' as the name of the charfield on your form. That'll cause save_m2m to think it needs to use the values in the charfield to set the related 'tag' field on the object.
Internally, save_m2m goes through each many-to-many field in the model. It checks for the presence of data under that name in the form's cleaned_data dictionary, and if present has the model field object update record the contents using the field's save_form_data method. It trusts the form field to have returned the right type of Python object. In this case, your charfield is returning a string (as expected), but it's incorrect to assign a string to a many-to-many field.
Related
When I load the page, the value of the input is automaticly this:
How is that possible?
views file
if request.method == 'POST':
form = FieldForm(request.POST, instance=Field(user=request.user))
if form.is_valid():
obj = form.save(commit=False)
obj.creator_adress = get_client_ip(request)
obj.save()
return redirect('/dashboard')
else:
form = FieldForm(instance=Field)
......
forms file
class FieldForm(forms.ModelForm):
class Meta:
model = Field
fields = (
'title',
'url'
)
models file
class Field(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
default=None,
null=True,
on_delete=models.CASCADE,
)
title = models.CharField(max_length=255)
url = models.CharField(max_length=255)
creator_adress = models.GenericIPAddressField(null=True)
def __str__(self):
return str(self.user)
Here one input 😂
value= <django.db.models.query_utils.DeferredAttribute object at 0x000001E180304250>
In your view you are passing a reference to your Field model as the argument instance. Doing this places the representation of the model fields into your form fields when it is rendered. If what you are wanting is just a blank form when you load the page then just remove the instance argument and create your form in your else statment, like form = FieldForm(). The instance argument is only needed for a form if you are trying to pre-populate data into the form, for example if you made a view where you wanted to update information on an already created object you would pass an instance of your model to the instance argument.
I am using django-bootstrap-modal-forms and it works perfectly as in documentation when using fields form my model. Some of the fields are ForeignKeys and they are displayed properly for user to select a value from database table that is referenced by the key, but instead of that I need to put username of the current user.
I tried to change how the CreateView class handles fields, but with no luck. Probably doing something wrong.
models.py
class userSchoolYear(models.Model):
user_in_school = models.ForeignKey(get_user_model(), null=True, on_delete=models.CASCADE)
school = models.ForeignKey(sifMusicSchool, on_delete=models.CASCADE)
school_year = models.ForeignKey(sifSchoolYear, on_delete=models.CASCADE)
school_year_grade = models.CharField(max_length=4, choices=IZBOR_RAZREDA, default=None, null=True)
user_instrument = models.ForeignKey(instType, on_delete=models.CASCADE, default=None, null=True)
user_profesor = models.ForeignKey(profSchool, on_delete=models.CASCADE, default=None, null=True)
views.py
class SchoolYearCreateView(BSModalCreateView):
template_name = 'school_year.html'
form_class = SchoolYearForm
success_message = 'Success!'
success_url = reverse_lazy('school')
def __init__(self, *args, **kwargs):
self.form_class.user_in_school = 'johnny' ### HERE
print(user.username)
super().__init__(*args, **kwargs)
forms.py
class SchoolYearForm(BSModalForm):
class Meta:
model = userSchoolYear
fields = '__all__'
Thanks to the author Uroš Trstenjak I was able to find a solution. I was wrong trying to set field values from views.py, instead it should be done in forms.py. So, basically I had to write a init for the form and alter fields values. Uroš pointed out that at from level I can get current user from self.request.user and it did work.
I have an abstract model class userabstract which has fields id(primary key), name(char field) and email(email field).
I am inheriting this class in two classes user, usertemp. After signing up, i want the data to be stored in usertemp. When user clicks on confirmation mail then that data will be transferred to user class.
But whats happening is, whenever someone signs up, usertemp model is updated instead of creating a new one. Same thing is happening with user class
Here is the code for models and views
class UserAbstract(models.Model):
id = models.AutoField(db_column='ID', primary_key=True, default=1) # Field name made lowercase.
name = models.CharField(db_column='NAME', max_length=100, default='') # Field name made lowercase.
email = models.CharField(db_column='EMAIL', max_length=100, default='') # Field name made lowercase.
def __str__(self):
return self.name
class Meta:
abstract = True
#python_2_unicode_compatible
class User(UserAbstract):
def __str__(self):
return self.name ;
class Meta:
managed = True
db_table = 'User'
#python_2_unicode_compatible
class Validation(models.Model):
key = models.AutoField(primary_key=True)
key_data = models.CharField(max_length=100, default='')
create_time = models.DateTimeField()
expire_time = models.DateTimeField()
def __str__(self):
return self.key_data
#python_2_unicode_compatible
class UserTemp(UserAbstract):
validation_key = models.ForeignKey(Validation, models.DO_NOTHING, related_name='+', default='') # Field name made lowercase.
verified = models.BooleanField(default=False)
def __str__(self):
return self.validation_key.key_data
views.py
def signup(request):
if request.method == 'POST':
form = FormTemp(request.POST, request.FILES)
if form.is_valid():
primary = form.cleaned_data['email']
try:
qdict = {}
qdict['email'] = primary
user = UserTemp.objects.get(**qdict)
if user.verified==True:
return HttpResponse("Account already exists")
except:
pass
email = form.cleaned_data['email']
signer = hashlib.sha256()
signer.update(primary)
validation_key = signer.hexdigest()
confirm_key = request.build_absolute_uri('/signup-confirm/')+'?key='+validation_key
send_mail('Confirm Your Mail', confirm_key, settings.EMAIL_HOST_USER, [email,])
valid = Validation(key_data=validation_key, create_time=datetime.now(), expire_time=datetime.now()+timedelta(days=30))
valid.save()
argsdict = {}
argsdict['name'] = form.cleaned_data['name']
argsdict['email'] = form.cleaned_data['email']
argsdict['validation_key'] = valid
argsdict['verified'] = False
usertemp = UserTemp(**argsdict)
usertemp.save()
return HttpResponse("Confirmation mail sent")
else:
return HttpResponse('Invalid Data')
else:
return HttpResponse('What are you doing here ? Tresspass')
The valid.save() is working fine and every time validation key is being saved but the usertemp contains only one model and that is the most recent one.
When i tried force_insert=True then its telling me that duplicate entry exist with same primary key. As you can see, the primary key field id is AutoField then why django not creating a new model when i am writing usertemp = UserTemp(**argsdict)
The problem here is that you've given your AutoField a default value. You're telling Django to assign that field the value 1 if you don't provide it, which means that you keep writing rows with the same id.
So just get rid of that default.
The broader point to understand is that defaults are a Django-level feature, while AutoField is a database-level feature. From the perspective of the database, there's no difference between explicitly assigned column values and Django default column values.
I have a situation again, when I do a form.save(), my form saves only the parent table, it does not save the intermediary table which is required for Many-To-Many relationships.
My models.py look like this
class Platform(models.Model):
id = models.AutoField(primary_key=True)
description = models.TextField(blank=True)
annotation_file_archived_location = models.FileField(upload_to='msrb/platform')
anntation_file_hashsum = models.TextField()
annotation = models.TextField(unique=True)
def __unicode__(self):
return self.annotation
class Meta:
managed = True
db_table = 'platform'
class Dataset(models.Model):
dataset_id = models.TextField(primary_key=True)
title = models.TextField(blank=True, null=True)
taxonomy = models.ForeignKey('Organism', blank=True, null=True)
citation = models.TextField(blank=True, null=True)
summary = models.TextField(blank=True, null=True)
contributor = models.TextField(blank=True, null=True) # This field type is a guess.
submitted = models.DateField(blank=True, null=True)
last_updated = models.DateField(blank=True, null=True)
author = models.ForeignKey('Users', db_column='author', blank=True, null=True)
platforms = models.ManyToManyField(Platform,through='DatasetPlatform')#,through_fields=('Platform:platform','dataset'))
class Meta:
managed = True
db_table = 'dataset'
class DatasetPlatform(models.Model):
id = models.IntegerField(primary_key=True)
platform = models.ForeignKey(Platform, null=False)
dataset = models.ForeignKey(Dataset,null=False)
class Meta:
managed = False
db_table = 'dataset_platform'
Forms.py
class DatasetForm(forms.ModelForm):
dataset_id = forms.CharField(required=True,help_text="dataset_id")
title = forms.CharField(required=True,help_text="title")
taxonomy = forms.ModelChoiceField(queryset=Organism.objects.all(),empty_label=None,help_text='Taxonomy')
citation = forms.CharField(required=True,help_text="citation")
summary = forms.CharField(required=True,help_text="summary")
contributor = forms.CharField(help_text="contributor (separated by comma)")
submitted = forms.DateField(initial = datetime.now,required=True,help_text="Submitted date")
last_updated = forms.DateField(initial = datetime.now,required=True,help_text="Last Updated date")
platform = forms.ModelMultipleChoiceField(queryset=Platform.objects.all(),help_text="Choose the platforms this dataset belongs to")
class Meta:
model = Dataset
fields = ('dataset_id','title','taxonomy','citation','summary','contributor','submitted','last_updated','platform')# Add author later ,'author')
views.py
def add_dataset(request):
context_dict = {}
if request.method == 'POST':
form = DatasetForm(request.POST)
if form.is_valid():
print "------------------------------------------------------------------------------"
print form.cleaned_data['platform']
form.save()
print "------------------------------------------------------------------------------"
return HttpResponseRedirect('/msrb/')
else:
print form
print form.errors
else:
form = DatasetForm()
context_dict['form'] = form
template = get_template('msrb/add_dataset.html')
context = RequestContext(request,context_dict)
return HttpResponse(template.render(context))
I have tried saving the data using
form.save(commit=True)
form.save_m2m()
form.cleaned_data gives the proper output.
I am not sure what am I missing here as I dont get an error message from django too.
EDIT
I have a workaround for the problem, but I am not sure if this is the best solution. If I can get a better solution, I will be greatful.
def add_dataset(request):
context_dict = {}
if request.method == 'POST':
form = DatasetForm(request.POST)
if form.is_valid():
print form.cleaned_data['platform']
f = form.save()
for p in form.cleaned_data['platform']: <--- Added
d = DatasetPlatform(dataset = f,platform = p) <--- Added
d.save() <--- Added
return HttpResponseRedirect('/msrb/')
else:
print form
print form.errors
else:
form = DatasetForm()
context_dict['form'] = form
template = get_template('msrb/add_dataset.html')
context = RequestContext(request,context_dict)
return HttpResponse(template.render(context))
Django is not able (well, refuses) to automatically save m2m relations with a custom through model. Saving the form data uses direct assignment to the ManyToManyField, which will not work as explained here.
If removing the custom through model is an option, I'd do that. Granted, it will have to be managed = True, but it greatly simplifies use of the field. You're not saving any extra data in the relationship, so it might be an option.
Otherwise, you have already found the only workaround. Each time you want to manipulate the m2m relationship, you'll have to manually create, alter and delete the DatasetPlatform instances. Again, this is explained in further detail in the relevant documentation.
I have this model:
class SearchPreference(models.Model):
"""Saves the preferred location and school_type of the User
"""
user = models.OneToOneField(User, related_name='search_preference')
location = models.ForeignKey(Location, null=True)
school_type = models.ForeignKey(SchoolType, null=True)
class Meta:
app_label = 'grants'
and this form:
class SearchPreferenceForm(forms.ModelForm):
location = forms.ChoiceField(queryset=Location.objects.all(),
to_field_name='slug',
required=False)
school_type = forms.ChoiceField(queryset=SchoolType.objects.all(),
to_field_name='slug',
required=False)
class Meta:
model = SearchPreference
fields = ('location', 'school_type')
I am trying to use the form to do validation of POST data, I am not displaying it in a template.
The problem is, the POST data can include a value which isn't in the Location or SchoolType table, so the form doesn't validate. The value is 'all', signifying 'all locations' or 'all school types', and I really want this to be saved as a SearchPreference with no location, i.e. location = null.
I could change 'all' to an empty value and that might work but then validation/logic has moved out of the form.
I thought I could use empty_value = 'all' but this doesn't work on a modelChoiceField.
Is there any way of doing this?
Your model needs blank=True as well and null=True
location = models.ForeignKey(Location, blank=True, null=True)
school_type = models.ForeignKey(SchoolType, blank=True, null=True)
This post talks about blank and null.
This worked in the end:
class SearchPreferenceForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(SearchPreferenceForm, self).__init__(*args, **kwargs)
self.fields['location'].empty_values.append('all')
self.fields['school_type'].empty_values.append('all')