Categories
Mastering Development

How to get QuerySet as TextChoice in Django

from django.conf import settings from django.db import models from django.utils import timezone class ShoppingList(models.Model): title = models.CharField(max_length=15) author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) created_date = models.DateField(default=timezone.now) def __str__(self): return self.title class ZutatenHinzufügen(models.Model): ingredient = models.CharField(max_length=15, name=”Zutat”) quantity = models.DecimalField(max_digits=5, decimal_places=2, name=”Menge”) author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, name=”Hinzugefügt durch”) created_date = models.DateTimeField(default=timezone.now, name=”Hinzugefügt am”) def __str__(self): return self.ingredient How […]

Categories
Mastering Development

Aggragate Value django

I would like to aggregate some values from a table in django. Show how many projects I`ve got in my table Aggregate values of the column budget Subtract two values between two objects: Tobeinvoiced = Budget – Invoiced views.py def index(request): projects = Project.objects.all() invoiced = project.objects.filter(type=’is_active’).aggregate(Sum(‘invoiced’))[‘invoiced’] return render(request, ‘projects/index_table.html’, {‘projects’: projects}) models.py class Project(models.Model): […]

Categories
Development

How to access Many-to-Many field data?

I have the following models: class Work_Music(MPTTModel, Work): name = models.CharField(max_length=10, null=True, blank=True) key = models.CharField(max_length=10, null=True, blank=True) tonality = models.CharField(max_length=20, null=True, blank=True) class WorkCast(models.Model): work = models.ForeignKey(Work_Music, verbose_name=_(‘work’), related_name=’workcast’, null=True, blank=True, on_delete=models.PROTECT) cast = models.ManyToManyField(Cast, verbose_name=_(‘cast’), related_name=’workcast’, blank=True) order = models.DecimalField(max_digits=100, decimal_places=2, null=True, blank=True) class Cast(models.Model): name = models.CharField(max_length=100, null=True, blank=True) In view.py, I […]

Categories
Development

Extraction datas from forms

I’ve just start to learn Django and now I’m trying understand how to insert datas in db, how to work with datas from templates and ect. My question is about extracting datas from form(Meta class) in template. Here is my code: model class Worker(models.Model): POSITION_SHOICE = ( (‘MANAGER’, ‘MANAGER’), (‘developer’, ‘DEVELOPER’), (‘teamlead’, ‘TEAMLEAD’), (‘pm’, ‘PM’), […]

Categories
Development

How to provide choices to django rest framework DecimalField serializer?

Django==2.2.6 djangorestframework==3.10.3 Models.py VAT_CHOICES = [(Decimal(‘0.00’), ‘0%’), (Decimal(‘6.00’), ‘6%’), (Decimal(‘12.00’), ‘12%’), (Decimal(‘25.00’), ‘25%’)] class Service(models.Model): vat = models.DecimalField(verbose_name=_(‘vat’), decimal_places=2, max_digits=10, choices=VAT_CHOICES) serializers.py class ServiceSerializer(serializers.ModelSerializer): vat = serializers.DecimalField(decimal_places=2, max_digits=10, coerce_to_string=True) If I do this then the value of vat in response is in srting as expected but the choices validation does not implied then. Now I […]

Categories
Development Django

Django / FactoryBoy – Overriding lazy_attributes

I’m working on a Django project for which I’ve created the following factory: class PurchaseFactory(factory.DjangoModelFactory): class Meta: model = ‘core.Purchase’ amount = decimal.Decimal(random.randrange(100, 100000) / 100) ( …. ) user = factory.SubFactory(UserFactory) @factory.lazy_attribute def date(self): if not self.date: return timezone.now() else: return self.date For which I’m running the following test: class TestGetBalanceForPeriod(TestCase): def setUp(self) -> […]

Categories
Development Django

Django Error: type object ‘Invoice_Line’ has no attribute ‘objects’

I defined a method in admin.py to add up after-tax amounts: def _grand_total(self, object_id): line_amounts = Invoice_Line.objects.filter(appointment_ref=object_id).values_list(‘total_after_tax’, flat=True) return reduce((lambda x,y: x + y), line_amounts) The ORM query defined in this line: Invoice_Line.objects.filter(appointment_ref=object_id).values_list(‘total_after_tax’, flat=True) generates the error below: type object ‘Invoice_Line’ has no attribute ‘objects’ Models code shown below: models.py: class Appointment(models.Model): create_date = models.DateField(auto_now_add=True, […]

Categories
API Development Django SQL

How would i replicate this SQL query in Django Rest Framework using generics.ListAPIView and serializers.ModelSerializer

Cant actually find anything like what i am trying to do with wanting to use case statements and left joins using DRF Django Rest Framework, yes this could be done on the front end of the project i am working on but id rather not have to let the front end potentially sending 100s of […]

Categories
Development

How to exclude a foreign’s key field from a form

Having my models Yacht and Offer, I created an intermediate table OfferHasYacht in order to connect a yacht with an offer. I have already created instances for both models. I want to exclude in my form the field date_created from the Offer model because when I am trying to included it in my form it […]

Categories
Development

Is there a way on Serializer perform create to save a field which is a Foreign Key

I have two models Timesheet and Employee now on my use case I want to save automatically the employee field of the timesheet using the perform_create but the api returns an error of “ValueError at /api/timesheet_entry/\nCannot assign \”, , ]>\”: \”Timesheet.employee\” must be a \”Employee\” instance. What I was trying to do is save the […]