I want to make pay form, but I cannot show produk in orederitem, please helping me to show orderitem :v. I am really newbie in here .
models.py
class Order(models.Model):
name = models.ForeignKey(Profile, on_delete=models.SET_NULL, blank=True, null=True)
order_data = models.DateTimeField(auto_now_add=True)
selesai = models.BooleanField(default=False, blank=True, null=True)
status = models.BooleanField(default=False, blank=True, null=True)
id_transaksi = models.CharField(max_length=200, null=True)
bukti = models.ImageField(null=True, blank=True)
ongkir = models.CharField(max_length=200, null=True)
total = models.CharField(max_length=200, null=True)
total_harga = models.CharField(max_length=200, null=True)
pembayaran = models.CharField(max_length=200, null=True)
class OrderItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, blank=True, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, blank=True, null=True)
quantity = models.IntegerField(default=0)
date_added = models.DateTimeField(auto_now_add=True)
views.py
def pembayaran(request):
customer = request.user.profile
ido = Order.objects.filter(id)
orders = Order.objects.filter(name=customer, selesai=True)
pengiriman = Pengiriman.objects.filter(name=customer)
OrderItems = OrderItem.objects.filter(order=ido)
print(OrderItems)
context = {'orders': orders,'pengiriman' :pengiriman , 'OrderItems': OrderItems }
return render(request, 'store/pembayaran.html', context)
I need to have multiple editable rows Header and Items of products and variants of products in Django Admin change_list.html / change_list_results.html like the image below:
Example result
this is my model:
class VariantProduct(models.Model):
product = models.ForeignKey(Product, verbose_name=_('Prodotto'), related_name='variants', on_delete=models.CASCADE)
sku = models.CharField(_('SKU'), max_length=40, blank=True, null=True,
help_text='SKU', unique=True)
ean_13 = models.BigIntegerField('EAN 13',
validators=[MaxValueValidator(9999999999999, "Ammessi massimo 13 numeri.")],
blank=True, null=True, help_text='Codice numerico EAN 13')
ean_128 = models.CharField('EAN-128', max_length=128, blank=True, null=True,
help_text='Codice alfanumerico 128')
qr_code = models.CharField("Codice QR", blank=True, null=True, max_length=255,
help_text="digitare il codice per la generazione del QR Code")
barcode = models.ImageField('Barcode', upload_to='barcode/', blank=True, null=True)
quantity = models.IntegerField('Quantità', blank=True, null=True)
class Meta:
verbose_name = _('prodotto')
verbose_name_plural = _("prodotti")
ordering = ['name']
class VariantProduct(models.Model):
product = models.ForeignKey(Product, verbose_name=_('Prodotto'), related_name='variants', on_delete=models.CASCADE)
sku = models.CharField(_('SKU'), max_length=40, blank=True, null=True,
help_text='SKU', unique=True)
ean_13 = models.BigIntegerField('EAN 13',
validators=[MaxValueValidator(9999999999999, "Ammessi massimo 13 numeri.")],
blank=True, null=True, help_text='Codice numerico EAN 13')
ean_128 = models.CharField('EAN-128', max_length=128, blank=True, null=True,
help_text='Codice alfanumerico 128')
qr_code = models.CharField("Codice QR", blank=True, null=True, max_length=255,
help_text="digitare il codice per la generazione del QR Code")
barcode = models.ImageField('Barcode', upload_to='barcode/', blank=True, null=True)
quantity = models.IntegerField('Quantità', blank=True, null=True)
qta_stock_add = models.PositiveSmallIntegerField('Q.ta +', help_text='Diminuisci N. Prodotti', blank=True,
null=True, )
qta_stock_sub = models.PositiveSmallIntegerField('Q.ta -', help_text='Diminuisci N. Prodotti', blank=True,
null=True, )
minimal_quantity = models.IntegerField('Scorta Minima', blank=True, null=True,
help_text='Scorta minima di magazzino, può essere negativa per gestione sotttottoscorta')
image = models.ImageField(upload_to='prodotti/%Y/%m/%d',
verbose_name=_('Immagine Variante'), blank=True, null=True)
state = models.BooleanField('Attivo', help_text='Stato della variante prodotto attivo/disattivo', default=True)
price = models.DecimalField(_('Prezzo'), max_digits=8, decimal_places=2, default=0.00)
dealer_price = models.DecimalField(_('Prezzo Rivenditore'), max_digits=8, decimal_places=2, default=0.00)
size = models.ForeignKey(Size, verbose_name=_('Taglia'), blank=True, null=True, on_delete=models.PROTECT)
color = models.ForeignKey(Color, verbose_name=_('colore'), blank=True, null=True, on_delete=models.PROTECT)
weight = models.DecimalField(_('Peso gr.'), blank=True, null=True, decimal_places=2, max_digits=10,
help_text=_('Peso in grammi'))
height = models.IntegerField(_('Altezza cm'), blank=True, null=True,
help_text=_('Altezza in grammi'))
width = models.IntegerField(_('Larghezza cm'), blank=True, null=True,
help_text=_('Larghezza in grammi'))
length = models.IntegerField(_('Lunghezza cm'), blank=True, null=True,
help_text=_('Lunghezza in grammi'))
class Meta:
verbose_name = _('Variante Prodotto')
verbose_name_plural = _('Varianti Prodotto')
def __str__(self):
return '{cod} - {name}'.format(cod=self.sku, name=self.product.name)
Is possible to show on django admin page the row of a product an relative rows of variants and make editable the fields of "quantity" on all rows?
Thanks in advance.
W.
I have a models
class TDebtor(models.Model):
first_name = models.CharField(max_length=250, blank=True, null=True)
surname = models.CharField(max_length=250, blank=True, null=True)
middle_name = models.CharField(max_length=250, blank=True, null=True)
iin = models.CharField(max_length=50, blank=True, null=True)
alive = models.BooleanField(blank=True, null=True)
birth_date = models.DateField(blank=True, null=True)
class TDebtData(models.Model):
agr_num = models.CharField(max_length=250, blank=True, null=True)
id_num = models.CharField(max_length=250, blank=True, null=True)
loan_amount = models.DecimalField(max_digits=15, decimal_places=2, blank=True, null=True)
principal_amount = models.DecimalField(max_digits=15, decimal_places=2, blank=True, null=True)
id_debtor = models.ForeignKey('TDebtor', models.DO_NOTHING, related_name="debtor_debt_data")
views.py
class ProductList(ListAPIView):
queryset = TDebtor.objects.prefetch_related(Prefetch('debtor_debt_data', queryset=TDebtData.objects.select_related('id_debtor')))
for i in queryset:
print(i.debtor_debt_data.agr_num)
serializer_class = TDebtorSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = ['iin', 'first_name', 'surname', 'middle_name', 'birth_date',
'alive']
if I'll make a filter with the fields of TDebtor it's okay, but how I can filter with agr_num?, I mean if I'll send agr_num=1238HFD32 it will return me TDebtor that has this agr_num
Use RELATED_NAME__FIELD. In your case debtor_debt_data__agr_num
class ProductList(ListAPIView):
...
filterset_fields = [..., `debtor_debt_data__agr_num`]
Docs in https://django-filter.readthedocs.io/en/stable/index.html
I've been asked to design a way for people to search through multiple models on criteria they enter and allow them to export any number of fields they select.
Example:
User enters "Teacher" as a term for Job Title and "Google" for a Work Site Location but want to export "Employee ID", "First Name", "Last Name", "Date of Birth"
I'm sure this is possible, but I'm at a complete loss for where to start.
My models (for reference) are here:
import datetime
from django.conf import settings
from django.db import models
from django.db.models import Q
from django.utils import timezone
class Classification(models.Model):
name = models.CharField(max_length=255, blank=False, null=False, verbose_name='Classification Name')
def __str__(self):
return '{}'.format(self.name)
class Meta:
db_table = 'Classification'
verbose_name = 'Classification'
verbose_name_plural = 'Classifications'
class Location(models.Model):
name = models.CharField(max_length=255, blank=False, null=False, verbose_name='Name')
aeries_id = models.CharField(max_length=25, blank=True, null=True, verbose_name='Aeries ID')
county_id = models.CharField(max_length=25, blank=True, null=True, verbose_name='County ID')
def __str__(self):
return '{}'.format(self.name)
class Meta:
db_table = 'Location'
verbose_name = 'Location'
verbose_name_plural = 'Locations'
ordering = ['name']
class Person(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, blank=True, null=True)
current_identity = models.ForeignKey('PersonIdentity', blank=True, null=True, on_delete=models.SET_NULL, verbose_name='Current Identity', related_name='current_identity')
employee_id = models.CharField(max_length=255, null=False, blank=False, verbose_name='Employee ID')
birthdate = models.DateField(blank=True, null=True, verbose_name='Birthdate')
original_hire_date = models.DateField(blank=True, null=True, verbose_name='Original Hire Date')
def __str__(self):
if self.current_identity is not None:
return '{}'.format(str(self.current_identity))
else:
return "{}".format(self.employee_id)
#property
def current_age(self):
from dateutil.relativedelta import relativedelta
difference_in_years = relativedelta(timezone.now().date(), self.birthdate).years
return difference_in_years
def primary_assignment(self):
return self.jobassignment_set.filter(Q(end_date__gte=timezone.now()) | Q(end_date=None)).order_by('priority')[0]
#property
def display_name(self):
if self.current_identity:
return '{0.first_name} {0.last_name} ({1.employee_id})'.format(self.current_identity, self)
else:
return 'Employee ID {0.employee_id}'.format(self)
#property
def is_certificated(self):
assignments = JobAssignment.objects.filter(Q(end_date__gte=timezone.now()) | Q(end_date=None)).filter(
person=self,
classification__name__icontains="Certificated"
)
return assignments.count() >= 1
class Meta:
db_table = 'Person'
verbose_name = 'Person'
verbose_name_plural = 'People'
class PersonIdentity(models.Model):
person = models.ForeignKey(Person, verbose_name='Person', on_delete=models.CASCADE)
first_name = models.CharField(max_length=50, blank=True, null=True, verbose_name='First Name')
middle_name = models.CharField(max_length=50, blank=True, null=True, verbose_name='Middle Name')
last_name = models.CharField(max_length=50, blank=True, null=True, verbose_name='Last Name')
start_date = models.DateField(blank=False, null=False, default=datetime.date.today, verbose_name='Start Date')
end_date = models.DateField(blank=True, null=True, verbose_name='End Date')
def __str__(self):
first_name = '' if self.first_name is None else self.first_name
last_name = '' if self.last_name is None else self.last_name
if self.middle_name is not None and self.middle_name != '':
return "{} {}. {} ({})".format(first_name, self.middle_name[0], last_name, self.person.employee_id)
else:
return "{} {} ({})".format(first_name, last_name, self.person.employee_id)
def save(self, *args, **kwargs):
change_identity = False
if not self.id:
change_identity = True
super().save(*args, **kwargs)
if change_identity:
self.person.current_identity = self
self.person.save()
class Meta:
db_table = 'PersonIdentity'
verbose_name = 'Person Identity'
verbose_name_plural = 'Person Identities'
ordering = ['end_date', 'last_name', 'first_name']
class Contact(models.Model):
person = models.OneToOneField(Person, on_delete=models.CASCADE, verbose_name='Person')
email_address = models.EmailField(blank=True, null=True, verbose_name='Primary Email')
phone_number = models.CharField(max_length=15, blank=True, null=True, verbose_name='Phone Number')
phone_extension = models.CharField(max_length=255, blank=True, null=True, verbose_name='Phone Extension')
address1 = models.CharField(max_length=255, blank=True, null=True, verbose_name='Address 1')
address2 = models.CharField(max_length=255, blank=True, null=True, verbose_name='Address 2')
city = models.CharField(max_length=255, blank=True, null=True, verbose_name='City')
state = models.CharField(max_length=2, default='CA', blank=True, null=True, verbose_name='State')
postal = models.CharField(max_length=5, blank=True, null=True, verbose_name='Postal Code')
def __str__(self):
return '{}'.format(self.person.current_identity)
class Meta:
db_table = 'Contact'
verbose_name = 'Contact'
verbose_name_plural = 'Contacts'
class Department(models.Model):
department_id = models.CharField(max_length=5, blank=False, null=False, verbose_name='Department ID')
description = models.CharField(max_length=255, blank=False, null=False, verbose_name='Department Description')
def __str__(self):
return '{}'.format(self.description)
class Meta:
db_table = 'Department'
verbose_name = 'Department'
verbose_name_plural = 'Departments'
class Job(models.Model):
name = models.CharField(max_length=255, blank=False, null=False, verbose_name='Name')
code = models.CharField(max_length=255, blank=False, null=False, verbose_name='Code')
def __str__(self):
return '{}'.format(self.name)
class Meta:
db_table = 'Job'
verbose_name = 'Job'
verbose_name_plural = 'Jobs'
class JobPosition(models.Model):
job = models.ForeignKey(Job, blank=True, null=True, verbose_name='Job')
reports_to = models.ForeignKey('JobPosition', blank=True, null=True, verbose_name='Reports To')
name = models.CharField(max_length=255, blank=True, null=True, verbose_name='Name')
number = models.CharField(max_length=255, blank=False, null=False, verbose_name='Number')
def __str__(self):
return '{}'.format(self.name)
class Meta:
db_table = 'JobPosition'
verbose_name = 'Job Position'
verbose_name_plural = 'Job Positions'
class JobAssignment(models.Model):
person = models.ForeignKey(Person, blank=False, null=False, verbose_name='Person')
position = models.ForeignKey(JobPosition, blank=False, null=False, verbose_name='Position')
location = models.ForeignKey(Location, blank=True, null=True, verbose_name='School')
classification = models.ForeignKey(Classification, blank=True, null=True, verbose_name='Classification')
start_date = models.DateField(blank=True, null=True, verbose_name='Start Date')
end_date = models.DateField(blank=True, null=True, verbose_name='End Date')
effective_date = models.DateField(blank=True, null=True, verbose_name='Effective Date')
fte = models.CharField(max_length=10, blank=True, null=True, verbose_name='FTE')
seniority_date = models.DateField(blank=True, null=True, verbose_name='Seniority Date')
last_start_date = models.DateField(blank=True, null=True, verbose_name='Last Start Date')
last_pay_date = models.DateField(blank=True, null=True, verbose_name='Last Pay Date')
entry_date = models.DateField(blank=True, null=True, verbose_name='Position Entry Date')
record_number = models.IntegerField(default=0, verbose_name='Position Record Number')
pay_group = models.CharField(max_length=3, blank=True, null=True, verbose_name='Pay Group')
indicator = models.CharField(max_length=1, blank=True, null=True, verbose_name='Indicator')
full_or_part_time = models.CharField(max_length=1, blank=True, null=True, verbose_name='Full/Part')
standard_hours_per_week = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, verbose_name='Standard Hours per Week')
pay_status = models.CharField(max_length=1, blank=True, null=True, verbose_name='Pay Status')
comp_rate = models.DecimalField(max_digits=15, decimal_places=6, blank=True, null=True, verbose_name='Comp Rate')
comp_frequency = models.CharField(max_length=10, blank=True, null=True, verbose_name='Comp Freq')
hourly_rate = models.DecimalField(max_digits=10, decimal_places=6, blank=True, null=True, verbose_name='Hrly Rate')
daily_rate = models.DecimalField(max_digits=10, decimal_places=6, blank=True, null=True, verbose_name='Daily Rt')
rate_code = models.CharField(max_length=10, blank=True, null=True, verbose_name='Rate Code')
compensation_rate = models.DecimalField(max_digits=15, decimal_places=6, blank=True, null=True, verbose_name='Compensation Rate')
total_cdays = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True, verbose_name='TOTAL_CDAYS')
teacher_hours = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True, verbose_name='Teacher Hours')
def __str__(self):
return '{} - {}'.format(self.position.name, self.position.number)
#classmethod
def current_assignments(cls):
return cls.objects.filter(Q(end_date__gte=timezone.now()) | Q(end_date=None)).exclude(person__ssn__isnull=True).exclude(person__current_identity__isnull=True).exclude(person__contact__isnull=True)
class Meta:
db_table = 'JobAssignment'
verbose_name = 'Job Assignment'
verbose_name_plural = 'Job Assignments'
ordering = ['location__name', 'person__current_identity__last_name', 'person__current_identity__first_name', 'position__name']