This is the model:
class PaymentChart(models.Model):
pharma = models.ForeignKey(PharmacyProfile , on_delete=models.CASCADE)
wholesale = models.ForeignKey(WholesaleProfile , on_delete=models.CASCADE)
amount = models.CharField(max_length=10)
reqeuest = models.BooleanField(default=False)
paid = models.BooleanField(default=False)
date = models.DateTimeField()
credit = models.BooleanField(default=False)
class Meta:
ordering = ['-date']
I want to filter the list of all months, .ie
[09/2021,10/2021,11/2021]
Try annotate, distinct, values_list and TruncMonth so:
from django.db.models.functions import TruncMonth
all_months = PaymentChart.objects.filter(paid=True).annotate(month=TruncMonth('date')).distinct('month').values_list('month', flat=True)
Source from Django-docs
Related
for example I have 3 classes defined as such
class Timecard(models.Model):
site_code = models.CharField(max_length=200)
contractor_name = models.CharField(max_length=200)
approved = models.BooleanField(default=False)
date = models.DateTimeField(default=timezone.now)
class Job(models.Model):
job_code = models.CharField(max_length=200)
description = models.CharField(max_length=200)
hourly_rate = models.DecimalField(max_digits=10, decimal_places=2)
max_hours = models.PositiveIntegerField()
time_card = models.ManyToManyField(Timecard, through="Timecard_Job")
class Timecard_Job(models.Model):
time_card = models.ForeignKey(Timecard, on_delete=models.CASCADE)
job = models.ForeignKey(Job, on_delete=models.CASCADE)
hours_worked = models.DecimalField(max_digits=10, decimal_places=2)
I have the following class governing my view
class FillTimecard(CreateView):
model = Timecard
form_class = CreateTimeCardForm
template_name = 'timesheet/timesheetSubmit.html'
success_url = reverse_lazy("index")
finally I have the following form class
class CreateTimeCardForm(forms.ModelForm):
class Meta:
model = Timecard
fields = ['site_code', 'contractor_name', 'date','job']
job = forms.ModelMultipleChoiceField(
queryset=Job.objects.all(),
widget=forms.Select
)
When I select the job from the drop down I want to also be able to enter the hours worked on that specific job and that be inserted into the join table.
If someone can just provide resources that will help me achieve this it would be appreciated.
Similar to what you have done with the job field, a new field can be added to your form as the docs explain here.
Essentially:
class CreateTimeCardForm(forms.ModelForm):
job = forms.ModelMultipleChoiceField(
queryset=Job.objects.all(),
widget=forms.Select
)
hours_worked = forms.DecimalField(label="Hours worked", min_value=0, decimal_places=2)
class Meta:
model = Timecard
fields = ['site_code', 'contractor_name', 'date']
Then you should implement clean() and save() methods in CreateTimeCardForm to be sure that the data is valid and to create the associations with other models, saving hours_worked.
I am trying to get the average with respect to the months of data in DRF. I have two problems here my values in the string so I need to convert first into double and then calculate it. I am not able to think any solution that's why posting the question without my solution what I have tried.
My modal class is:
class FeedDataValues(models.Model):
sensor = models.ForeignKey(
'core.SensorDevice',
on_delete=models.SET_NULL,
null=True
)
hardware_id = models.CharField(max_length=255)
field1 = models.CharField(max_length=255, null=True)
field2 = models.CharField(max_length=255, null=True)
field3 = models.CharField(max_length=255, null=True)
received_at = models.DateTimeField(null=True)
My serializer class is:
class MQTTFeedDataSerializer(serializers.ModelSerializer):
class Meta:
model = models.FeedDataValues
fields = ('id','sensor','hardware_id','field1','field2','received_at',)
And views is:
class MQTTFeedDataListView(generics.ListAPIView):
authentication_classes = (authentication.TokenAuthentication,)
permission_classes = (permissions.IsAuthenticated, permissions.IsAdminUser,)
serializer_class = serializers.MQTTFeedDataSerializer
queryset = models.FeedDataValues.objects.all()
filter_backends = (DjangoFilterBackend, OrderingFilter,)
filter_class = filters.MQTTFeedValuesFilter
Any suggestion will be of great help.
Note: IF INFORMATION BELOW IS NOT CLEAR TO UNDERSTAND - PLEASE ASK ME, I WILL UPDATE AND POST INFORMATION YOU NEED | It is important for me
In Warehouse(models.Model) I have amount attribute and
in ChosenProduct(models.Model) - quantity
I'm trying to get amount in Warehouse through chosen_products instance in App_formSerializer to add the quantity of chosen_product
But I can not get the chosen_products objects from instance
--> below Out:
class WarehouseSerializer(serializers.ModelSerializer):
category_name = serializers.ReadOnlyField(
source='category_product.category_name')
posted_user = serializers.ReadOnlyField(
source='posted_user.username')
class Meta:
model = Warehouse
fields = ['id', 'category_product', 'category_name', 'condition',
'product_name', 'amount', 'barcode', 'f_price', 'created_at', 'updated_at', 'posted_user']
class ChosenProductSerializer(serializers.ModelSerializer):
product_info = WarehouseSerializer(source='product', read_only=True)
period_info = Product_periodSerializer(source='period', read_only=True)
class Meta:
model = ChosenProduct
exclude = ('app_form',)
class App_formSerializer(serializers.ModelSerializer):
chosen_products = ChosenProductSerializer(many=True)
def update(self, instance, validated_data):
instance.terminated = validated_data.get('terminated', instance.terminated)
if instance.terminated == True :
print('-----------TRUE--------------------')
print(instance.chosen_products)
print('-----------PRINT--------------------')
instance.save()
return instance
class Meta:
model = App_form
fields = '__all__'
Out
-----------TRUE--------------------
creditapi.ChosenProduct.None
-----------PRINT--------------------
QUESTION UPDATED
models.py
class Warehouse(models.Model):
category_product = models.ForeignKey(
Category_product, on_delete=models.CASCADE)
product_name = models.CharField(max_length=200, unique=True)
condition = models.BooleanField(default=False)
amount = models.IntegerField()
barcode = models.BigIntegerField()
f_price = models.CharField(max_length=255, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
posted_user = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
def __str__(self):
return self.product_name
class App_form(models.Model):
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,12}$', message="Phone number must be entered in the format: '998981234567'. Up to 12 digits allowed.")
terminated = models.BooleanField(default=False)
name = models.CharField(max_length=150)
phone_number = models.CharField(validators=[phone_regex], max_length=13)
def __str__(self):
return self.surname
class ChosenProduct(models.Model):
product = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
quantity = models.IntegerField()
app_form = models.ForeignKey(App_form, on_delete=models.CASCADE, related_name='chosen_products')
def __str__(self):
return self.product.product_name
If you write instance.chose_products you access the manager, not the QuerySet that contains the items. You can use .all() to obtain the QuerySet with all the objects:
print(instance.chosen_products.all())
If you access a ForeignKey in reverse, you have a manager, since zero, one, or more elements can refer to the instance.
You can for example aggregate over the chose_products, for example if you want to retrieve the number of related chose_products, you can use:
print(instance.chosen_products.count())
I would however advise not store (aggregated) data in the App_form, but aggregate data when you need it. Data duplication is an anti-pattern, and it turns out it is hard to keep data in sync.
I am trying to create in a nested serializer in Django Rest Framework but I keep getting this error. I know why it's throwing it but, i thought Django would handle the one-to-many relationship.
AttributeError at /api/campaigns
Got AttributeError when attempting to get a value for field ads on serializer CampaignSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the Campaign instance.
Original exception text was: 'Campaign' object has no attribute 'ads'.
class Campaign(models.Model):
name = models.CharField(max_length = 30)
type = models.CharField(max_length = 20)
start_date = models.DateField(auto_now=False, auto_now_add=True)
end_date = models.DateField(auto_now=False, auto_now_add=True)
locations = models.CharField(max_length = 30)
budget = models.IntegerField()
land_page = models.URLField()
keywords = models.TextField()
CPM = models.IntegerField()
CPC = models.IntegerField()
description = models.TextField()
commission = models.IntegerField()
pay_off = models.IntegerField()
advertiser = models.ForeignKey(User)
date_time = models.DateField(auto_now=False, auto_now_add=True)
class Meta:
db_table = "campaigns"
class ADS(models.Model):
advertiser = models.ForeignKey(User)
campaign = models.ForeignKey(Campaign)
headline = models.CharField(max_length=50)
description_1 = models.TextField(blank=True)
description_2 = models.TextField(blank=True)
display_url = models.URLField(blank=True)
final_url = models.URLField(blank=True)
mobile_url = models.URLField(blank=True)
class Meta:
db_table = "ads"
These are my serializers
class ADSerializer(serializers.ModelSerializer):
adsImages = AdsImagesSerializer(read_only=True,many=True)
class Meta:
model = ADS
fields = ("headline","description_1","description_2","display_url","final_url","mobile_url","advertiser","adsImages")
class CampaignSerializer(serializers.ModelSerializer):
advertiser = AdvertiserProfile(read_only=True,required=False)
ads = ADSerializer(many=True)
class Meta:
model = Campaign
fields = ("name","type","start_date","end_date","locations","budget","land_page","keywords","CPM","CPC","description","commission","pay_off","ads","advertiser",)
def get_validation_exclusions(self, *args, **kwargs):
exclusions = super(CampaignSerializer,self).get_validation_exclusions()
return exclusions + ['advertiser']
def create(self, validated_data):
ads_data = validated_data.pop('ads')
campaign = Campaign.objects.create(**validated_data)
for ad_data in ads_data:
ADS.objects.create(campaign=campaign, **ad_data)
return campaign
I just fixed this, with a related name in the model
campaign = models.ForeignKey(Campaign, related_name="ads")
I think it didn't worked last time because the reverse relationship was not working without a related_name.
Models:
class Applicant_Skill(models.Model):
user = models.ForeignKey(User)
#applicant = models.ForeignKey(Applicant)
skill = models.ForeignKey('skills_layer.Skill')
active = models.BooleanField(default=True)
class Job_Posting(models.Model):
company = models.ForeignKey('companies_layer.Company', default=-1)
job_posted_by = models.ForeignKey(User, default=-1)
job_title = models.CharField(max_length=100)
job_summary = HTMLField(blank=True)
job_details = HTMLField(blank=True)
no_of_openings = models.IntegerField(default=0)
tags = models.CharField(max_length=200)
experience_min = models.IntegerField(default=0)
experience_max = models.IntegerField(default=0)
job_location = models.ForeignKey('meta_data_layer.Location', blank=True, null=True)
qualification = models.ForeignKey('meta_data_layer.Qualification', default=-1)
specialization = models.ForeignKey('meta_data_layer.Specialization', default=-1)
nationality = models.ForeignKey('meta_data_layer.Nationality', default=-1)
live = models.BooleanField(default=True)
closing_date = models.DateField(default=datetime.date.today())
auto_renew = models.BooleanField(default=False)
active = models.BooleanField(default=True)
class Meta:
verbose_name = "job posting"
def __str__(self):
return self.job_title
Resource:
from tastypie.resources import ModelResource
from job_posting_layer.models import Job_Posting
from companies_layer.models import Company
from django.contrib.auth.models import User
import meta_data_layer
from tastypie import fields
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
def dehydrate(self, bundle):
bundle.data['logged_user_id'] = bundle.request.user.id
return bundle
class JobListingResource(ModelResource):
#company = fields.ForeignKey(CompanyResource,'company', full=True)
#job_posted_by = fields.ForeignKey(UserResource,'job_posted_by', full=True)
company_name = fields.CharField(attribute="company__company_name", null=True)
company_id = fields.CharField(attribute="company__id", null=True)
user_first_name = fields.CharField(attribute="job_posted_by__first_name", null=True)
user_last_name = fields.CharField(attribute="job_posted_by__last_name", null=True)
user_id = fields.CharField(attribute="job_posted_by__id", null=True)
job_location = fields.CharField(attribute="job_location__location_name", null=True)
job_city = fields.CharField(attribute="job_location__city", null=True)
qualification = fields.CharField(attribute="qualification__qualification_degree", null=True)
specialization = fields.CharField(attribute="specialization__specialization_course", null=True)
nationality = fields.CharField(attribute="nationality__country_name", null=True)
class Meta:
queryset = Job_Posting.objects.all()
resource_name = 'jobs'
Today is the 1st day I am trying Tastypie so please be kind with me :(
The JobListingResource returns all the Job Listings. But I want to get only those Job Listings for which the Tags column contains values from the skill column of the logged in user.
Eg: If user "A" has logged in and has the following skills "python,django,jquery". I want the JobListingResource to return only those records which contains [python/django/jquery] in the tags column.
I'm assuming you know how to do the queries and just need to know where to do it in Tastypie. In your JobListResource override as follows:
def get_object_list(self, request):
# get all the jobs according to the queryset in Meta
base = super(JobListingResource, self).get_object_list(request)
# and add a filter so only users ones appear
user = request.user
skills = query to get all the skills for the user
return base.filter(filter to apply to JobPosting to only return jobs matching skills list)