I have a model like this:
class Entity(models.Model):
entity_name = models.CharField(max_length=100)
entity_id = models.CharField(max_length=30, primary_key=True)
entity_parent = models.CharField(max_length=100, null=True)
photo_id = models.CharField(max_length=100, null=True)
username = models.CharField(max_length=100, null=True)
date_matched_on = models.CharField(max_length=100, null=True)
status = models.CharField(max_length=30, default="Checked In")
def __unicode__(self):
return self.entity_name
class Meta:
app_label = 'match'
ordering = ('entity_name','date_matched_on')
verbose_name_plural='Entities'
I also have a view like this:
def photo_match(request):
''' performs an update in the db when a user chooses a photo '''
form = EntityForm(request.POST)
form.save()
And my EntityForm looks like this:
class EntityForm(ModelForm):
class Meta:
model = Entity
My template's form returns a POST back to the view with the following values:
{u'username': [u'admin'], u'entity_parent': [u'PERSON'], u'entity_id': [u'152097'], u'photo_id': [u'2200734'], u'entity_name': [u'A.J. McLean'], u'status': [u'Checked Out'], u'date_matched_on': [u'5/20/2010 10:57 AM']}
And form.save() throws this error:
Exception in photo_match: The Entity could not be changed because the data didn't validate.
I have been trying to figure out why this is happening, but cannot pinpoint the exact problem. I can change my Entities in the admin interface just fine. If anybody has a clue about this I would greatly appreciate it!
Thanks,
Igor
If the entity you are trying to update is already saved, then you need to provide an instance parameter when you bind the form, otherwise save will try to perform an INSERT rather than an UPDATE, and the new object won't validate (check out the django docs here).
Try:
def photo_match(request):
''' performs an update in the db when a user chooses a photo '''
entity = Entity.objects.get(pk=request.POST['entity_id'])
form = EntityForm(request.POST, instance=entity)
form.save()
You'll want to be a little more robust about the way you look up the entity, of course.
Related
Is there any way to change a field value (related to a foreign key) in a Django ModelForm once the form is initialized and filled by the user (I'm using request.POST). I want to change the value when the user doesn't select any option of the dropdown list. I tried this formulari_mostra.data['pools'] = 1 in views.py after saving the feedback from the form with no result:
def sample_form(request):
formulari_mostra=FormulariMostra()
if request.method=="POST":
formulari_mostra=FormulariMostra(request.POST or None)
if formulari_mostra.is_valid():
feedback = formulari_mostra.save(commit=False)
sample = Sample.objects.all()
feedback.sample = sample
feedback.save()
formulari_mostra.save_m2m()
formulari_mostra.data['pools'] = 1
messages.success(request, 'Mostra enregistrada correctament!')
return render(request, "sample/formulari_mostra.html", {'formulari':formulari_mostra})
I got this message:
This QueryDict instance is immutable
I know I can set an initial (default) before introducing data in the form but I don't want to have the default option highlighted in the dropdown.
My model:
class Sample(models.Model):
id_sample = models.AutoField(primary_key=True)
name = models.CharField(unique=True, max_length=20)
sample_id_sex = models.ForeignKey(Sex, on_delete=models.CASCADE, db_column='id_sex', verbose_name='Sexe')
indexes = models.ManyToManyField(Index, through='SamplePoolIndexCand', through_fields=('sample_id', 'index_id'), blank=True, verbose_name="Índexs")
pools = models.ManyToManyField(Pool, through='SamplePoolIndexCand', through_fields=('sample_id', 'pool_id'), blank=True, verbose_name="Pools")
gene_cand_lists = models.ManyToManyField(GeneCandList, through='SamplePoolIndexCand', through_fields=('sample_id', 'gene_cand_list_id'), blank=True, verbose_name="Llista de gens candidats")
class Meta:
db_table = 'sample'
def __str__(self):
return self.name
My forms.py:
class FormulariMostra(ModelForm):
class Meta:
model = Sample
fields = ("name", "sample_id_sex", "pools",)
first - you are setting the polls property after save() is called, so even if this would work, you are not saving the change.
second - if you want to set the polls property to model, then set it to the model instead of the form (formulari_mostra). I dont know how your models look like, so I can only assume the model which has the pools property is in the variable feedback, so you want to do:
feedback = formulari_mostra.save(commit=False)
feedback.pools = 1
feedback.save()
I've got the following Situation, I have a rather large legacy model (which works nonetheless well) and need one of its fields as a distinct dropdown for one of my forms:
Legacy Table:
class SummaryView(models.Model):
...
Period = models.CharField(db_column='Period', max_length=10, blank=True, null=True)
...
def __str__(self):
return self.Period
class Meta:
managed = False # Created from a view. Don't remove.
db_table = 'MC_AUT_SummaryView'
Internal Model:
class BillCycle(models.Model):
...
Name = models.CharField(max_length=100, verbose_name='Name')
Period = models.CharField(max_length=10, null=True, blank=True)
Version = models.FloatField(verbose_name='Version', default=1.0)
Type = models.CharField(max_length=100, verbose_name='Type', choices=billcycle_type_choices)
Association = models.ForeignKey(BillCycleAssociation, on_delete=models.DO_NOTHING)
...
def __str__(self):
return self.Name
Since I don't want to connect them via a Foreign Key (as the SummaryView is not managed by Django) I tried a solution which I already used quite a few times. In my forms I create a ModelChoiceField which points to my Legacy Model:
class BillcycleModelForm(forms.ModelForm):
period_tmp = forms.ModelChoiceField(queryset=SummaryView.objects.values_list('Period', flat=True).distinct(),
required=False, label='Period')
....
class Meta:
model = BillCycle
fields = ['Name', 'Type', 'Association', 'period_tmp']
And in my view I try to over-write the Period Field from my internal Model with users form input:
def billcycle_create(request, template_name='XXX'):
form = BillcycleModelForm(request.POST or None)
data = request.POST.copy()
username = request.user
print("Data:")
print(data)
if form.is_valid():
initial_obj = form.save(commit=False)
initial_obj.ModifiedBy = username
initial_obj.Period = form.cleaned_data['period_tmp']
initial_obj.Status = 'Creating...'
print("initial object:")
print(initial_obj)
form.save()
....
So far so good:
Drop Down is rendered correctly
In my print Statement in the View ("data") I see that the desired infos are there:
'Type': ['Create/Delta'], 'Association': ['CP'], 'period_tmp': ['2019-12']
Still I get a Select a valid choice. That choice is not one of the available choices. Error in the forms. Any ideas??
my form in forms.py is then passed to this method in my views.py, if I go into python shell and print objects from MyProfile, all of the fields show values except for nearbyzips, which shows None. As you can see below, I am trying to manually assign a value to nearbyzips when the form is saved.
inside views.py
#secure_required
#login_required
def profile_edit(request, username, edit_profile_form=EditProfileForm,
template_name='userena/profile_form.html', success_url=None,
extra_context=None, **kwargs):
profile = get_profile(user)
form = edit_profile_form(instance=profile, initial=user_initial)
if request.method == 'POST':
if form.is_valid()
cleanzipcode = form.cleaned_data['zipcode']
nearestzips = PostalCode.objects.distance(PostalCode.objects.get(code=cleanzipcode).location)
zip_codes = list(nearestzips.values_list('code', flat=True))
//print zip_codes
form.cleaned_data['nearbyzips'] = zip_codes
//print form.cleaned_data['nearbyzips']
profile=form.save()
return redirect(redirect_to)
models.py
class MyProfile(UserenaBaseProfile):
user = models.OneToOneField(User,
unique=True,
verbose_name=_('user'),
related_name='my_profile')
streetaddress=models.CharField(null=True, blank=True, max_length=30)
city = models.CharField(null=True, blank=True, max_length=20)
state = models.CharField(null=True, blank=True, max_length=20)
zipcode = models.IntegerField(_('zipcode'),
max_length=5, null=True, blank=True)
nearbyzips = models.IntegerField(null=True, blank=True, max_length=100)
phone=models.CharField(null=True, blank=True, max_length=16)
websiteurl=models.CharField(null=True, blank=True, max_length=38)
Something to keep in mind, if I go into python shell and run:
nearestzips = PostalCode.objects.distance(PostalCode.objects.get(code='97202').location
print nearestzips
It prints all the Postal Codes I would expect. So I'm not sure where exactly is broken. I don't see any errors in my logs.
UPDATE:
I have added print statements in my views. printing zip_codes and form.cleaned_data['nearbyzips'] both show:
[u'97202', u'97206', u'97214', u'97215', u'97239']
But it still does not appear to be saving to the form.
2 things stand out to me here.
Your form is created for some kind of profile model (get_profile_model()) -- does this profile model have a field called nearbyzips?
If your model does have a field called nearbyzips, explicitly include it (and all the fields you want to update) in a tuple/list of fields in your form class's inner Meta class.
Also, I don't see you calling the save method on your form class in your view function (i.e. form.save()).
Change this line:
tzips = PostalCode.objects.distance(PostalCode.objects.get(code='cleanzipcode').location)
to this:
tzips = PostalCode.objects.distance(PostalCode.objects.get(code=cleanzipcode).location)
I've been trying to solve this problem for a couple of days now, getting quite desperate. See the commented out code snippets for some of the things I've tried but didn't work.
Problem: How can I limit the values in the category field of the IngredientForm to only those belonging to the currently logged in user?
views.py
#login_required
def apphome(request):
IngrFormSet = modelformset_factory(Ingredient, extra=1, fields=('name', 'category'))
# Attempt #1 (not working; error: 'IngredientFormFormSet' object has no attribute 'fields')
# ingrformset = IngrFormSet(prefix='ingr', queryset=Ingredient.objects.none())
# ingrformset.fields['category'].queryset = Category.objects.filter(user=request.user)
# Attempt #2 (doesn't work)
# ingrformset = IngrFormSet(prefix='ingr', queryset=Ingredient.objects.filter(category__user_id = request.user.id))
models.py:
class Category(models.Model):
name = models.CharField(max_length=30, unique=True)
user = models.ForeignKey(User, null=True, blank=True)
class Ingredient(models.Model):
name = models.CharField(max_length=30, unique=True)
user = models.ForeignKey(User, null=True, blank=True)
category = models.ForeignKey(Category, null=True, blank=True)
counter = models.IntegerField(default=0)
forms.py:
class IngredientForm(ModelForm):
class Meta:
model = Ingredient
fields = ('name', 'category')
UPDATE: I've made some progress but the solution is currently hard-coded and not really usable:
I found out I can control the categoryform field via form class and then pass the form in the view like this:
#forms.py
class IngredientForm(ModelForm):
category = forms.ModelChoiceField(queryset = Category.objects.filter(user_id = 1))
class Meta:
model = Ingredient
fields = ('name', 'category')
#views.py
IngrFormSet = modelformset_factory(Ingredient, form = IngredientForm, extra=1, fields=('name', 'category'))
The above produces the result I need but obviously the user is hardcoded. I need it to be dynamic (i.e. current user). I tried some solutions for accessing the request.user in forms.py but those didn't work.
Any ideas how to move forward?
You don't need any kind of custom forms. You can change the queryset of category field as:
IngrFormSet = modelformset_factory(Ingredient, extra=1, fields=('name', 'category'))
IngrFormSet.form.base_fields['category'].queryset = Category.objects.filter(user__id=request.user.id)
Category.objects.filter(user=request.user)
returns a list object for the initial value in your form which makes little sense.
Try instead
Category.objects.get(user=request.user)
or
Category.objects.filter(user=request.user)[0]
For my first Django project I'm trying to make an app that lets users create lists of media (books, movies, etc.) with various fields describing each object (title, author, etc.), and I'm having trouble figuring out how to get it to save. That is to say that nothing happens when the form is submitted. Can someone tell me what I'm doing wrong? Sorry if this is a bit of a noob question; it seems like I'm missing something really basic here. (I'm using basic HTML forms instead of ModelForms because for some media types I want to ignore certain fields - e.g. "author" for movies - but if there is an easy way to do that using ModelForms, I'm all ears.)
from views.py:
def editbook(request,list_owner,pk):
book_list = Item.objects.all().filter(item_creator=list_owner).filter(category='book').order_by('type','name')
item_to_edit = Item.objects.get(pk=pk)
if request.method == 'POST':
item_to_edit.save()
return render_to_response('books.html', {'booklist': book_list, 'listowner': list_owner}, RequestContext(request))
else:
form=EditItem()
return render_to_response('editbook.html', {'listowner': list_owner, 'item_to_edit': item_to_edit}, RequestContext(request))
from models.py:
CATEGORY_CHOICES = (
('book','book'),
('tv','tv'),
('movie','movie'),
('game','game'),
('music','music'),
)
class Item(models.Model):
item_creator = models.CharField(max_length=30) # user name goes here
category = models.CharField(max_length=5, choices=CATEGORY_CHOICES)
name = models.CharField(max_length=70)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
artist = models.CharField(max_length=70, blank=True)
type = models.CharField(max_length=50, blank=True)
progress = models.CharField(max_length=10, blank=True)
finished = models.BooleanField(default=False)
rating = models.IntegerField(default=0, blank=True, null=True)
comment = models.CharField(max_length=140, blank=True)
def __unicode__(self):
return self.name
There is, of course, a way to only use some fields in a modelform: as fully documented in Using a subset of fields on the form, you can use the fields or exclude attributes in the form's Meta class.
However you'll still need, as szaman points out, to pass the POST data to the form and check for validity, and in addition you'll need to pass in the instance paramater as you're updating an existing instance.
What I see is that you get object from database and when form is submitted than just saving the object, but you don't update any field so you cannot see changes in db. Try to do:
if request.method == "POST":
form = MyForm(request.POST)
logging.info("form.is_valid() %s" % form.is_valid())
if form.is_valid():
item_to_edit.name = form.cleaned_data['name']
item_to_edit.save()
...