Django: How to create a abstract field? - python

I would like to start using an abstract model for my application (based on a django-accounting app). How should I create my field on the views.py. I guess I will also need to change my view file when creating a new field...Can I still create the same way I used to if it was not an abstract model?
views.py
#login_required(login_url="/login/")
def create_bill(request):
form = BillForm(request.POST or None, request.FILES or None)
if form.is_valid():
bill = form.save(commit=False)
bill.save()
return render(request, 'accounting/bills/detail.html', {'bill': bill})
context = {
"form": form,
}
return render(request, 'accounting/bills/create_bill.html', context)
#login_required(login_url="/login/")
def detail_bill(request, bill_id):
user = request.user
bill = get_object_or_404(Bill, pk=bill_id)
return render(request, 'accounting/bills/detail.html', {'bill': bill, 'user': user})
#login_required(login_url="/login/")
def bill_update(request, bill_id):
bill = get_object_or_404(Bill, pk=bill_id)
form = BillForm(request.POST or None, instance=bill)
if form.is_valid():
form.save()
return render(request, 'accounting/bills/index_table.html', {'bill': bill})
else:
form = BillForm(instance=bill)
return render(request, 'accounting/bills/edit.html', {'form': form})
models.py
class AbstractSale(CheckingModelMixin, models.Model):
number = models.IntegerField(default=1,db_index=True)
# Total price needs to be stored with and wihtout taxes
# because the tax percentage can vary depending on the associated lines
total_incl_tax = models.DecimalField("Total (inc. tax)",decimal_places=2,max_digits=12,default=D('0'))
total_excl_tax = models.DecimalField("Total (excl. tax)",decimal_places=2,max_digits=12,default=D('0'))
# tracking
date_issued = models.DateField(default=date.today)
date_dued = models.DateField("Due date",blank=True, null=True,help_text="The date when the total amount ""should have been collected")
date_paid = models.DateField(blank=True, null=True)
class AbstractSaleLine(models.Model):
label = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
unit_price_excl_tax = models.DecimalField(max_digits=8,decimal_places=2)
quantity = models.DecimalField(max_digits=8,decimal_places=2,default=1)
class Meta:
abstract = True
class Bill(AbstractSale):
organization = models.ForeignKey('Organization',on_delete=models.CASCADE, related_name="bills", verbose_name="To Organization")
client = models.ForeignKey('contacts.Client', on_delete=models.CASCADE,verbose_name="From client")
payments = GenericRelation('Payment')
class BillLine(AbstractSaleLine):
bill = models.ForeignKey('Bill',related_name="lines",on_delete=models.CASCADE)
tax_rate = models.ForeignKey('TaxRate',on_delete=models.CASCADE)
class Meta:
pass

if you use class-based-views (CBV) you can use inheritance as found in pure python.
Note that you have not defined a Meta class (and with abstract=True) within your AbstractSale class. By not doing this, you will end up with an additional table in your database. For more info see here.

Related

django / filter the fields of a form and have the error 'int' object has no attribute '_meta'

I use django-cities-light for a travel website and I would like to filter the cities in the fields ville_de_depart and ville_destination in the newBookingForm by trip.depart and trip.destination.
I tried to pass the trip object in the instance of newBookingForm. I override the __init__ and I took the value of the depart and destination, I succeeded in filtering the fields but I could no longer save the newBooking, the view redirect to the alltrip page with no error but no new booking is added to the database.
I tried to replace the trip by the slug which is the same value as the id and it shows me this error
'int' object has no attribute '_meta'
models.py
class trip(models.Model):
depart = models.ForeignKey(default='',to=Country,on_delete=models.CASCADE,related_name='depart')
destination = models.ForeignKey(default='',to=Country,on_delete=models.CASCADE)
date_de_depart = models.DateField(default='')
prix_kg = models.PositiveIntegerField(default='')
collecte = models.BooleanField(default=False,null=False,help_text='' )
creation_date = models.DateTimeField(auto_now=True)
author = models.ForeignKey(to=settings.AUTH_USER_MODEL,on_delete=models.CASCADE,default='')
slug = models.SlugField(max_length=100, default='' )
#jouter slug
def save(self, *args , **kwargs):
super(trip, self).save(*args , **kwargs)
if not self.slug:
self.slug = self.id
self.save()
def __str__(self):
return f'{self.id} {self.author} '
class Booking(models.Model):
trip = models.ForeignKey(trip,on_delete=models.CASCADE, default='',)
author = models.ForeignKey(to=settings.AUTH_USER_MODEL,on_delete=models.CASCADE,default='')
creation_date = models.DateTimeField(auto_now=True)
ville_de_depart = models.ForeignKey(City,on_delete=models.CASCADE,default='')
slug = models.SlugField(max_length=100, default='' )
# ville_depart = models.ForeignKey(default='',to=City,on_delete=models.CASCADE,related_name='ville_dep')
sender_phone = PhoneNumberField(blank=True)
receiver_phone = PhoneNumberField()
ville_destination = models.ForeignKey(default='',to=City,on_delete=models.CASCADE,related_name='ville_dest')
#jouter slug
def save(self, *args , **kwargs):
super(Booking, self).save(*args , **kwargs)
if not self.slug:
self.slug = self.id
self.save()
def __str__(self):
return str(self.trip.author)
views.py
def detailsTrip(request, slug):
trip = get_object_or_404(models.trip,slug=slug)
auth = trip.author
bookingForm = newBookingForm(instance=slug)
context = {'trip': trip, 'auth': auth, 'form': bookingForm}
if request.method == 'POST':
form = newBookingForm(request.POST , instance=slug )
if request.user.is_authenticated:
if form.is_valid():
trip = get_object_or_404(models.trip,slug=slug)
Booking = form.save(commit=False)
Booking.trip_id= trip.id
Booking.author_id = request.user.id
Booking = form.save()
return redirect('/alltrips')
else:
trip = get_object_or_404(models.trip,slug=slug)
auth = trip.author
bookingForm = newBookingForm()
context = {'trip': trip, 'auth': auth, 'form': bookingForm}
return render(request, 'detailstrip.html', context)
else:
return render (request, 'notFound.html')
return render(request,'detailstrip.html', context , )
forms.py
class newBookingForm(forms.ModelForm,):
def __init__(self,*args,**kwargs):
# capture the instance : Slug
slug = kwargs.get('instance')
# capture current trip
Trip = get_object_or_404(trip, id=slug)
# Filter cities field by the instance : trip.depart / trip.destination
super(newBookingForm, self).__init__(*args,**kwargs)
self.fields['ville_de_depart'].queryset = City.objects.filter(country_id=Trip.depart)
self.fields['ville_destination'].queryset = City.objects.filter(country_id=Trip.destination)
class Meta:
model = Booking
fields = ['ville_de_depart','ville_destination']
exclude = ['sender_phone','receiver_phone']
solution
#view.py
def detailsTrip(request, slug):
trip = get_object_or_404(models.trip,slug=slug)
auth = trip.author
#add the trip to the form
bookingForm = newBookingForm(trip=trip)
context = {'trip': trip, 'auth': auth, 'form': bookingForm}
if request.method == 'POST':
#add the object trip to the form here too
form = newBookingForm(trip,request.POST )
if request.user.is_authenticated:
if form.is_valid():
trip = get_object_or_404(models.trip,slug=slug)
Booking = form.save(commit=False)
Booking.trip_id= trip.id
Booking.author_id = request.user.id
Booking = form.save()
return redirect('/alltrips')
else:
trip = get_object_or_404(models.trip,slug=slug)
auth = trip.author
bookingForm = newBookingForm()
context = {'trip': trip, 'auth': auth, 'form': bookingForm}
return render(request, 'detailstrip.html', context)
else:
return render (request, 'notFound.html')
return render(request,'detailstrip.html', context , )
form.py
class newBookingForm(forms.ModelForm,):
class Meta:
model = Booking
fields = ['ville_de_depart','ville_destination']
exclude = ['sender_phone','receiver_phone']
def __init__(self,trip,*args,**kwargs):
# Filter cities field by trip.depart / trip.destination
super(newBookingForm, self).__init__(*args,**kwargs)
self.fields['ville_de_depart'].queryset = City.objects.filter(country=trip.depart)
self.fields['ville_destination'].queryset = City.objects.filter(country=trip.destination)
As long as you use many-to-many fields, you need to call form.save_m2m() after saving the model instance. or you can just call form.save() without commit=False keyword argument.
Here are some quotes from Django documentation:
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.
Another side effect of using commit=False is seen when your model has a many-to-many relation with another model. If your model has a many-to-many relation and you specify commit=False when you save a form, Django cannot immediately save the form data for the many-to-many relation.
read this documentation about the save method

how to request and post an object to a foreignkey field

When I do this exactly as provided below, a shipping address object is created without the customer assigned in the shipping address foreignkey field, I can add it from the admin panel manually but I'm not able to make it work through code, idk what I'm doing wrong, please help!
**models.py**
class Customer(models.Model):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, blank=True, null=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField(max_length=150)
class ShippingAddress(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, blank=True, null=True)
address_one = models.CharField(max_length=200)
address_two = models.CharField(max_length=200)
...
**views.py**
def checkout(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
else:
items = []
order = {'get_cart_total': 0, 'get_cart_items': 0}
if request.method == 'POST':
form = ShippingForm(request.POST)
if form.is_valid():
#how do I get the customer to get added in the foreignkey field for the shipping address model
form.save()
return redirect('store:checkout_shipping')
else:
form = ShippingForm()
else:
form = ShippingForm()
context = {"items": items, "order": order, "form": form}
return render(request, 'store/checkout.html', context)
In response to your comment #how do I get the customer to get added... etc, in the case that your ShippingForm() points to your ShippingAddress model, or at least something with a customer foreign key field, you may need to do something like this:
def checkout(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
else:
items = []
order = {'get_cart_total': 0, 'get_cart_items': 0}
if request.method == 'POST':
form = ShippingForm(request.POST)
if form.is_valid():
new_shipment = form.save(commit=False)
new_shipment.customer = customer
new_shipment.save()
return redirect('store:checkout_shipping')
else:
form = ShippingForm()
else:
form = ShippingForm()
context = {"items": items, "order": order, "form": form}
return render(request, 'store/checkout.html', context)
Using commit=False on the form.save() will allow you to subsequently modify other fields, in this case, by adding the customer relation, then saving it. More information here in the Django documentation. Salient quote:
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. In this case, it’s up to you to call save() on the resulting
model instance. This is useful if you want to do custom processing on
the object before saving it, or if you want to use one of the
specialized model saving options. commit is True by default.
"Custom processing" in this case is the creation of the foreign key relationship to the model instance (customer).

How to assign value of field of one model to field of another model?

Say, I have a view function that creates a Vacancy object on saving the form, and it has the field company that, if the form is valid, has to be assigned with value of field name, that is on the other model named EmployerProfile, how can I do that? I have company as a foreign key on my model.
My models
class EmployerProfile(models.Model):
name = models.CharField(max_length = 64)
description = models.TextField()
username = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)
class Vacancy(models.Model):
name = models.CharField(max_length = 64)
competences = models.CharField(max_length = 32)
salary = models.DecimalField(decimal_places = 2, max_digits = 10)
description = models.TextField(null = True)
company = models.ForeignKey(EmployerProfile, on_delete = models.CASCADE)
featured = models.BooleanField(default = False)
My view
#login_required(login_url = 'login')
def vacancyAddView(request):
if request.method == 'POST':
form = VacancyAddForm(request.POST)
if form.is_valid():
form.save()
return redirect('profile')
else:
form = VacancyAddForm()
context = {
'form':form
}
return render(request, "addVacancy.html", context)
My form
class VacancyAddForm(forms.ModelForm):
class Meta:
model = Vacancy
fields = [
'name',
'competences',
'description',
'salary',
]
P.S. I have tried adding this piece of code to my view, rigth after form.is_valid():
obj = EmployerProfile.objects.get(username = request.user)
form.instance.company = obj.name
but it didn't work, it returned the ValueError with text "Vacancy.company" must be a "EmployerProfile" instance.
P.P.S. I also tried to override the save method in my model like this
def save(self, *args, **kwargs):
self.company = self.EmployerProfile.name
super(Vacancy, self).save(*args, **kwargs)
but it didn't work either.
You can't do what you want. Your Vacancy model defines company as a foreign key, but you're trying to set it as a string.
class Vacancy(models.Model):
company = models.ForeignKey(EmployerProfile, on_delete = models.CASCADE)
However, here's what your view should change to be to work with your models as you've defined them currently.
#login_required(login_url = 'login')
def vacancyAddView(request):
if request.method == 'POST':
form = VacancyAddForm(request.POST)
if form.is_valid():
# Set the company
form.instance.company = request.user.employerprofile
form.save()
return redirect('profile')
else:
form = VacancyAddForm()
context = {
'form':form
}
return render(request, "addVacancy.html", context)
The basic issue here is that you can't set a ForeignKey field to be a string. If you want the user to be able to set the Company from the form, you have a couple of choices. One is to take what you have now in the form and use it to filter, e.g. something like:
company = EmployerProfile.objects.filter(name__iexact=form.cleaned_data['company'])
form.instance.company = company
Another would be to change the form so that instead of a text input, company is a ModelChoiceField, or a regular ChoiceField populated with the company choices available to the user.
In either case the end goal will be to ensure that the company field contains a foreign key to an EmployerProfile object, rather than the string name of a company.

Django python setting initial values dynamically to form

I'm building a small web service for inventory control. As part of this, I want to populate a detail view for any of the inventory items. This is what I have so far for that:
class Product_Update(forms.Form):
Product_Code = forms.CharField(
max_length=10,
attrs={"placeholder = <ID here> Readonly = True"
)
Name = forms.CharField(max_length=100)
Description = forms.Textarea(attrs={"Rows": 3})
price = forms.DecimalField()
mini = forms.IntegerField()
Max = forms.IntegerField()
How do I pass the form the parameters?
You should use a ModelForm instead:
class ProductUpdate(forms.Form):
class Meta:
model = Product
fields = ('product_code', 'name', 'description', 'price', 'mini', 'max')
Now you can easily pass a model instance to your form:
def some_view(request):
instance = Product.objects.first()
form = ProductUpdate(request.POST or None, instance=instance)
context = {'form':form}
return render(request, 'some_template.html', context)
If you want to show multiple products in the same form, you will need to use modelformset_factory:
from django import forms
ProductFormSet = forms.modelformset_factory(Product, form=ProductUpdate, extra=0)
Now in your views.py, you can pass a QuerySet to your form:
def some_view(request):
queryset = Product.objects.all()
form = ProductFormSet(request.POST or None, queryset=queryset)
if request.method == 'POST' and form.is_valid():
form.save()
context = {'form':form}
return render(request, 'some_template.html', context)
You can access the form's data in the view by accessing request.POST
def actionView(request, product_id):
product = Product.objects.get(id=product_id)
form = ProductUpdate(request.POST, instance=product_id)
form.save(commit=False) #Do this if you want to make changes to some value
form.price = 112233
updated_form = form.save()

How would I create a form for a foreign key field that has a drop down menu with an 'add item' option in django?

I'll start with my model fields:
class Store(models.Model):
name = models.CharField(max_length=250)
def __str__(self):
return self.name
class Product(models.Model):
type = models.CharField(max_length=250)
def __str__(self):
return self.type
class Receipt(models.Model):
store = models.ForeignKey(Store)
date = models.DateField()
line_items = models.ManyToManyField(Product, through='ReceiptProduct')
def __str__(self):
return self.store.name + ': ' + str(self.date)
class ReceiptProduct(models.Model):
receipt = models.ForeignKey(Receipt)
product = models.ForeignKey(Product)
price = models.FloatField()
description = models.CharField(max_length=500, null=True, blank=True)
def __str__(self):
return self.product.type
What I would like to do is create a form for the ReceiptProduct model.
class AddItemForm(ModelForm):
class Meta:
model = ReceiptProduct
fields = ['product', 'price', 'description']
Done. And the view?
def add_receipt_product(request, receipt_id):
current_receipt = Receipt.objects.get(id=receipt_id)
if request.method != 'POST':
# No data submitted; create a blank form.
form = AddItemForm(initial={'receipt': current_receipt})
else:
# POST data submitted; process data.
form = AddItemForm(data=request.POST)
if form.is_valid():
new_product = form.save(commit=False)
new_product.receipt = current_receipt
new_product.save()
return HttpResponseRedirect(reverse('purchase_log:receipt_details', args=[receipt_id]))
context = {'current_receipt': current_receipt, 'form': form}
return render(request, 'purchase_log/add_receipt_product_form.html', context)
Okay, so what I would like to do is, under the 'product' field (which is a drop down menu populated by the Product model), have an option called, maybe, 'custom product' or something, that the user can select to add an item to the Product model and will then appear in future drop down menus. Is this do-able?
Thank you all in advanced!!
Django implements this in terms of a "formset". Check out this tutorial for additional information: http://whoisnicoleharris.com/2015/01/06/implementing-django-formsets.html I think the example there is fairly similar to yours.
In the Django admin interface, things are somewhat easier, and you can use an Inline.

Categories

Resources