I am getting this error but I don't know why.
models.py
class Year(models.Model):
year = models.CharField(max_length=5, unique=True)
class Meta:
ordering = ['-year']
def __str__(self):
return self.year
class Photo(models.Model):
title = models.CharField(max_length=64)
description = models.CharField(max_length=255)
created = models.DateTimeField(auto_now_add=True)
image = models.ImageField(upload_to='photos/')
thumbnail = ResizedImageField(blank=True, size=[360, 360], force_format='JPEG', upload_to='thumbnails/')
submitter = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
year = models.ForeignKey(Year, blank=True, on_delete=models.CASCADE)
views.py
def photo_create_view(request):
form = AddPhotoForm()
if request.method == 'POST':
image = request.FILES['image']
thumbnail = request.FILES['image']
title = request.POST.get('title')
description = request.POST.get('description')
year = request.POST.get('year')
people = request.POST.get('people')
tags = request.POST.get('tags')
photo = Photo(image=image, thumbnail=thumbnail, title=title, description=description, year=year,
people=people, tags=tags, submitter=request.user,)
photo.save()
return redirect('/photo/?page=1')
return render(request, 'photoapp/create.html', context={'form':form})
Cannot assign "123": "Photo.year" must be a "Year" instance. I have checked the Year table and year.id 123 exists. What am I missing?
year_id = int(request.POST.get('year'))
Photo(year_id=year_id, ...)
It's got to be the physical Year object
year = Year.objects.get_or_create(year=request.POST.get('year'))
Notes:
You could also use .get() or .filter().first(), must be the object and not a QuerySet
If you use a form you can get away with just the Pk in the request.POST
My own two sense: I don't think there's a benefit of having Year as it's own table, but maybe you're just using placeholders
Related
TO SAVE DATA that is inputted in form in Django i tried tomake it like this
I put this in my model.py
class Item(models.Model):
CATEGORY = (
('Gudang Kering', 'Gudang Kering'),
('Gudang Basah','Gudang Basah'),
)
name = models.CharField(max_length=200,null= True)
stock = models.IntegerField(default='0', blank=False, null=True)
category = models.CharField(max_length=200,null= True,choices=CATEGORY)
reorderlevel = models.IntegerField(default='0', blank=False, null=True)
maxreorderlevel = models.IntegerField(default='0', blank=False, null=True)
description = models.CharField(max_length=200,null= True, blank= True)
date_created = models.DateTimeField(auto_now_add= True)
tags = models.ManyToManyField(Tag)
def __str__(self):
return self.name
class Issue(models.Model):
STATUS = (
('Pending', 'Pending'),
('Granted','Granted'),
('Denied','Denied'),
)
customer = models.ForeignKey(Customer, null=True, on_delete= models.SET_NULL)
item = models.ForeignKey(Item, null=True, on_delete= models.SET_NULL)
quantity = models.IntegerField(default='0', blank=False, null=True)
date_created = models.DateTimeField(auto_now_add=True, auto_now=False)
status = models.CharField(max_length=200,null= True, choices=STATUS)
Then in view.py i define the form like this
def updateIssue(request, pk):
issue = Issue.objects.get(id=pk)
item = Item.objects.all()
form = UpdateIssueForm(instance=issue)
if request.method == 'POST':
form = UpdateIssueForm(request.POST,instance=issue)
#print ('printing:',request.POST)
if form.is_valid():
instance = form.save(commit=False)
if instance.status == 'Granted':
item.stock -= instance.quantity
instance.save()
item.save()
else:
instance.save()
return redirect('/')
context = {'form':form}
return render(request,'accounts/issue_form.html',context)``
The Goal
if instance == "Granted"
the item.stock will be decreased on the amount of instance.quantity
and will be saved.
else
instance will just be saved without affecting the stock from the 2nd model
The error
item = Item.objects.all()
even when called the item.stock have 0 attribute even when i have input data in database for that table
There is no need to get Item since we can access the Item related to Issue using the Issue object like issue.item. This is just an example from docs:
Article model has a field reporter which is a ForeignKey realted to Reporter model. Using Article object the Reporter object is accessed.
>>> new_article = r.article_set.create(headline="John's second story", pub_date=date(2005, 7, 29))
>>> new_article
<Article: John's second story>
>>> new_article.reporter
<Reporter: John Smith>
>>> new_article.reporter.id
1
Like so, we can access the Item using Issue
def updateIssue(request, pk):
issue = Issue.objects.get(id=pk) # we have our Issue here
form = UpdateIssueForm(instance=issue)
if request.method == 'POST':
form = UpdateIssueForm(request.POST,instance=issue)
if form.is_valid():
instance = form.save(commit=False)
if instance.status == 'Granted':
issue.item.stock -= instance.quantity # access Item by using Issue object's related field with name item
issue.item.save() # save the Item first
instance.save() # then the Issue
else:
instance.save()
return redirect('/')
context = {'form':form}
return render(request,'accounts/issue_form.html',context)
I'm creating a dashboard to edit a tour app.
Per tour I have a child record in which I define steps. The 2 models look like this:
models.py
class Tour(models.Model):
tour_id = models.CharField(primary_key=True,unique=True, max_length=10)
country = models.ForeignKey(Countries, models.DO_NOTHING, db_column='country')
language = models.ForeignKey(Language, models.DO_NOTHING, db_column='language')
lastupddtm = models.DateTimeField(default=timezone.now)
productid = models.CharField(max_length=50)
title = models.CharField(max_length=50)
description = models.CharField(max_length=100)
descrlong = models.CharField(max_length=1000)
live = models.CharField(max_length=1)
image = models.ImageField(upload_to=upload_tour_image, storage=OverwriteStorage(), blank=True, null=True)
class Meta:
db_table = 'tour'
verbose_name_plural = "tour"
def get_language_flag(self):
return self.language.flag.url
def __str__(self):
return str(self.tour_id) + ' - ' + str(self.title) + ' - ' + str(self.description)
class Toursteps(models.Model):
# tour_id = models.OneToOneField(Tour, models.DO_NOTHING, db_column='tour_id')
tour = models.ForeignKey(Tour, related_name='toursteps', on_delete=models.CASCADE)
step = models.IntegerField(unique=True)
title = models.CharField(max_length=50)
description = models.CharField(max_length=100)
descrlong = models.CharField(max_length=1000)
audiotext = models.TextField()
latitude = models.FloatField()
longitude = models.FloatField()
radius = models.FloatField()
image = models.ImageField(upload_to=upload_tour_step_image, blank=True, null=True)
class Meta:
db_table = 'tourSteps'
verbose_name_plural = "tourSteps"
def __str__(self):
return str(self.tour) + "|" + str(self.step)
After I created a Tour, I go to a detail page. From there I can click a link to add a step for this tour.
This is where the problem is. I pass the tour_id as a variable into the url, but I can't find a way to pick it up in the CreateView of the step.
urls.py
urlpatterns = [
path('tour/<str:pk>/detail', views.TourDetailView.as_view(), name='tour_detail'),
path('tour/<str:pk>/edit', views.UpdateTourView.as_view(), name='tour_edit'),
path('tour/<str:pk>/remove', views.DeleteTourView.as_view(), name='tour_remove'),
path('tour/<str:tour_id>/step/new', views.CreateTourStepView.as_view(), name='tour_step_new')
]
Tour detail view
<p><span class="glyphicon glyphicon-plus"></span></p>
views.py
class CreateTourStepView(LoginRequiredMixin,CreateView):
login_url = '/login/'
redirect_field_name = 'tour_admin/tour_list.html'
success_url = '/'
form_class = TourStepForm
model = Toursteps
def get_context_data(self, **kwargs):
context = super(CreateTourStepView, self).get_context_data(**kwargs)
print(context['tour_id'])
return context
forms.py
class TourStepForm(forms.ModelForm):
class Meta():
model = Toursteps
#fields = '__all__'
exclude = ('tour',)
def form_valid(self, form):
if form.is_valid():
form.instance.tour_id = self.request.GET("tour_id")
form.instance.save()
return HttpResponseRedirect(self.get_success_url())
def get_success_url(self):
return reverse('tour_detail', kwargs={'pk':form.instance.tour_id})
First, your form_valid() and get_success_url() methods belong in your view, not in your form.
Second, the tour_id is passed to the view's kwargs, it's not a query parameter, hence not in self.request.GET. You can find it in self.kwargs.
Third, you need to actually fetch the Tour from your database, not just assign the tour_id. I could post to any tour_id if I wanted and there's no guarantee the tour_id belongs to an actual Tour object. Return a 404 if the tour doesn't exist. And if it exists, assign it to the tour step.
Finally, you should not assign to and save form.instance. You should get the instance using step = form.save(commit=False), then assign to step and save step.
I want to be able to have a user update two models with one submit button. The first model will house all of the book titles (unique) and pages that users submit. The other will show which users submitted which books.
Ideally, I'd like to do something like this:
if request.method == "POST":
form = AddBookForm(request.POST)
if form.is_valid():
books = form.save(commit=False)
ub = UserBooks()
books.book_title = form.cleaned_data['book_title']
books.book_total_pages = form.cleaned_data['book_total_pages']
ub.user = request.user
ub.book_title = form.cleaned_data['book_title']
ub.save()
books.save()
return redirect('new_book')
But that's giving me the error:
Cannot assign "'Some Book Title'": "UserBooks.book_title" must be a
"Books" instance.
What would be the best way to update two models with one form?
Here are the other files.
models.py
class Books(models.Model):
created = models.DateTimeField(auto_now_add=True, editable=False)
last_updated = models.DateTimeField(auto_now=True, editable=False)
book_title = models.CharField(max_length=100, unique=True)
book_total_pages = models.IntegerField()
class Meta:
ordering = ('-created',)
def __str__(self):
return '{0}'.format(self.book_title)
class UserBooks(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False, blank=False)
book_title = models.ForeignKey(Books, on_delete=models.CASCADE, null=False, blank=False)
views.py
def new_book(request):
user = request.user
if request.method == "POST":
form = AddBookForm(request.POST)
if form.is_valid():
books = form.save(commit=False)
books.book_title = form.cleaned_data['book_title']
books.book_total_pages = form.cleaned_data['book_total_pages']
books.save()
return redirect('new_book')
else:
form = AddBookForm()
return render(request, 'main/addanewbook.html', {
'form': form,
'allBooks': allBooks,
'userbooks': userbooks,
})
forms.py
class AddBookForm(forms.ModelForm):
class Meta:
model = Books
fields = ('book_title', 'book_total_pages')
you need to change a bit in the view
if form.is_valid():
books = form.save(commit=False)
ub = UserBooks()
books.book_title = form.cleaned_data['book_title']
books.book_total_pages = form.cleaned_data['book_total_pages']
books = books.save()
ub.user = request.user
ub.book_title = books
ub.save()
return redirect('new_book')
this will do it
So here, I would like the quantity existing of my Article model to increase when saving the Purchase model,
Here is my code in views.py that does not work!
I am still a beginner in Django. thank you in advance
example:
quantity of article in stock: 20
quantity purchased during a purchase: 5
so in the end in the database I would like to have 25 in the item warren in stock!
sorry for my english, i use google translator
def achat_form_view(request):
if (request.method == 'POST'):
form = AchatForm(request.POST,error_class=ParagraphErrorList)
if form.is_valid():
Article.quantite = Article.quantite + Achat.quantite_a
form.save(commit=True)
return redirect('manapoitra_achat')
else:
form = AchatForm()
return render(request, 'achatH.html', {'form': form})
models.py :
class Achat(models.Model):
id_article_a = models.ForeignKey(Article, on_delete=models.CASCADE)
id_fournisseur_a = models.ForeignKey(Fournisseur, on_delete=models.CASCADE)
quantite_a = models.PositiveIntegerField(max_length=4, verbose_name="Quantité(s)")
date_a = models.DateTimeField(auto_now_add=True, verbose_name="Date de création")
date_save_tara_a = models.DateField(blank=True, null=True)
def __unicode__(self):
return self.pk+' achat'
class Article(models.Model):
photo = models.FileField()
nom = models.CharField(max_length=60, verbose_name="Produit")
type = models.ForeignKey(Type, verbose_name="Type", on_delete=models.CASCADE)
categorie = models.ForeignKey(Categorie, verbose_name="Catégorie", on_delete=models.CASCADE)
prix_de_vente = models.CharField(max_length=8, verbose_name="Prix de vente")
prix_d_achat = models.CharField(max_length=8, verbose_name="Prix d'achat")
quantite = models.PositiveIntegerField(max_length=4, verbose_name="Quantité(s)")
date_a = models.DateTimeField(auto_now_add=True, verbose_name="Date de création")
date_de_perim = models.DateField(blank=True, null=True, verbose_name="Perimé(e) le")
def __str__(self):
return self.nom
The problem with your code is that Achat and Article refer to the entire class, not any specific instance. What you want to do is take the Achat created by your form, and increase the quantity of the specific Article chosen in that form. You can do this via the return value of form.save(), which is an instance of Achat.
if form.is_valid():
achat = form.save()
article = achat.id_article_a
article.quantite += achat.quantite_a
article.save()
return redirect('manapoitra_achat')
(Note, your field naming convention is very strange; there's no need to suffix with _a, but more importantly you should not name ForeignKey fields with an id_ prefix; the Django ForeignKey is not an ID, but gives you access directly to the related object. So for example id_article_a should be just article.)
Here is my model:
class Browser(models.Model):
profile_name = models.CharField(max_length=400)
browser_type = (
('fr', 'Firefox'),
('ch', 'Chrome'),
('op', 'Opera'),
('ot', 'Other'),
)
browser_name = models.CharField(choices=browser_type, max_length=2)
device_name = models.CharField(max_length=400)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Bookmark(models.Model):
browser = models.ForeignKey(Browser, on_delete=models.CASCADE, null=True, blank=True)
title = models.TextField()
url = models.TextField()
iv = models.TextField()
salt = models.TextField()
iteration = models.IntegerField(default=1500)
tags = TaggableManager()
I only want to update certain fields, so here is the modelform
class BookmarkFormEdit(ModelForm):
class Meta:
model = Browser
exclude = ('tags', 'browser_name', 'device_name', 'profile_name')
but my problem is, values are not updating as expected . Here is the view:
def bookmark_edit(request, pk=None):
if request.method == 'POST':
bookmark = Bookmark.objects.get(pk=pk)
frm = BookmarkFormEdit(request.POST, instance=bookmark)
print(request.POST.get('iteration')) // printing correct value from front-end
if frm.is_valid():
x = frm.save()
print(x.iteration) // not saving the new value !
return JsonResponse({'status': 'created'})
else:
return JsonResponse({'error': frm.errors})
return render(request, 'bookmark_edit.html', {'pk': pk})
You are incorrectly defined model in the form. You should use Bookmark model instead of Browser.
class BookmarkFormEdit(ModelForm):
class Meta:
model = Bookmark
You may need to define fields to include/exclude as you want for this model.