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)
Related
I have 3 models and I am trying to create a dashboard with a list of Trials that spans all Client Sessions for a specific client chosen via a filter.
Here are the models:
class Trial(models.Model):
behavior_name = models.ForeignKey(Behavior, on_delete=models.CASCADE)
client_session = models.ForeignKey(Client_Session, on_delete=models.CASCADE)
frequency_input = models.PositiveIntegerField(default=0, blank=True)
duration_input = models.DurationField(blank=True, default=timedelta(minutes=0))
class Meta:
verbose_name_plural = 'trials'
def __str__(self):
return str(self.id)
class Client_Session(models.Model):
name = models.CharField(max_length=200, null=False)
session_date = models.DateField(blank=False,null=False)
client = models.ForeignKey(Client, on_delete=models.CASCADE)
behaviors = models.ManyToManyField(Behavior, null=False)
therapist = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
class Meta:
verbose_name_plural = 'clientsessions'
def __str__(self):
return self.name
class Client(models.Model):
#user = models.ForeignKey(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
date_of_birth = models.DateField(blank=True, null=True)
gender = models.CharField(max_length=10, choices=GENDER_CHOICES,blank=True)
gaurdian_first_name = models.CharField(max_length=200, blank=True)
gaurdian_last_name = models.CharField(max_length=200, blank=True)
diagnosis = models.CharField(max_length=200, choices=DIAGNOSIS_CHOICES, blank=True)
therapist = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
def __str__(self):
return self.last_name
Here is the view that im trying to create
def dashboard(request):
# filter list of client sessions by selected client
client_sessions = Client_Session.objects.filter(therapist=request.user)
client = DashboardClientFilter(request.GET, queryset=client_sessions)
client_sessions = client.qs
#Create list of Trials across Client Sessions for the filtered client
trial_list = Trial.objects.filter(client_session__client=client)
You have an error filter query you used Filter data. Expected is queryset.
Change,
trial_list = Trial.objects.filter(client_session__client=client)
into
client_sessions = client.qs
client_pks = client.qs.values_list('client_id',flat=True)
trial_list = Trial.objects.filter(client_session__client_id__in= list(client_pks))
trial_list = Trial.objects.filter(client_session__client__in= list(client_pks)).distinct() distinct() method is used to remove duplicated but it not work some databases
I am work on a Django Project where I have Profile and submited_apps models. The profile model holds details such as applicant, nation, state, phone etc whereas the submited_apps models only records the users whose application were submitted successfully with a applicant field, Universal Unique International Id and date.
How do I have a dependent search form for nation and state and be able to search submited_apps model for selected nation, state and display the result in pagination.
Profile Model Code below
class Profile(models.Model):
applicant = models.OneToOneField(User, on_delete=models.CASCADE, null = True)
surname = models.CharField(max_length=10, null=True)
othernames = models.CharField(max_length=30, null=True)
gender = models.CharField(max_length=6, choices=GENDER, blank=True, null=True)
nation = models.CharField(max_length=10, choices=NATION, blank=True, null=True)
state = models.CharField(max_length=20, null=True)
address = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=11, null=True)
image = models.ImageField(default='avatar.jpg', upload_to ='profile_images')
Submitted Model Code below"
class submited_apps(models.Model):
applicant = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
application = models.UUIDField(primary_key = True, editable = False, default=uuid.uuid4)
confirm = models.BooleanField()
date = models.DateTimeField(auto_now_add=True)
def save(self, *args, **kwargs):
self.application == str(uuid.uuid4())
super().save(*args, **kwargs)
def __unicode__(self):
return self.applicant
def __str__(self):
return f'Application Number: {self.application}-{self.applicant}'
ModelForm code below:
class Applicant_Search_Form(forms.ModelForm):
class Meta:
model = submited_apps
fields = ['applicant']
Here is my view for the search
def SearchApplicants(request):
context = {}
searchForm = Applicant_Search_Form(request.POST or None)
if searchForm:
list_applicants = submited_apps.objects.filter(applicant__iexact=[searchForm['applicant'].value()])
else:
list_applicants= submited_apps.objects.all()
paginator = Paginator(list_applicants, 5)
page = request.GET.get('page')
paged_listApps = paginator.get_page(page)
context.update({
'list_applicants':paged_listApps,
'searchForm':searchForm,
})
return render(request, 'user/search_applicants_nation.html',context)
My problem is that I am getting this error message upon load of the plage.
Related Field got invalid lookup: icontains
I'm trying to filter API responses through a web search. After typing a search query it doesn't filter the result. I'm also using pagination. How to solve this do I need to make changes in my filter or View.
search query
http://127.0.0.1:8000/course-api/subjectlist/?search="ICT"/
after giving this search query still it returns all of the elements through the response.
Subject model
class Subject(models.Model):
def upload_location(instance, filename):
return "subject_images/%s/%s" % (instance.subject_name, filename)
subject_name = models.CharField(max_length=200, null=True)
subject_cover = models.ImageField(null=True, blank=True, upload_to=upload_location,default="subject_images/default.png")
description = models.CharField(max_length=500, null=True, blank=True)
author = models.ForeignKey(TeacherProfile,on_delete=models.CASCADE,null=True,default=None)
subject_type = models.CharField(max_length=100, null=True, blank=True)
class_type = models.CharField(max_length=10, null=True, blank=True)
short_description = models.CharField(max_length=300,blank=True,null=True)
created_at = models.DateTimeField(default=now)
filters.py
import django_filters
from ..models import Subject
class SubjectFilter(django_filters.FilterSet):
class Meta:
model = Subject
fields = ['subject_name']
Views.py
#api_view(['GET'])
def SubjectList(request):
paginator = PageNumberPagination()
paginator.page_size=5
subjects =SubjectFilter ( request.GET, queryset= Subject.objects.all())
result_page = paginator.paginate_queryset(subjects.queryset,request)
serializer = SubjectViewSerializer(result_page,many=True)
return paginator.get_paginated_response(serializer.data)
I have this models on django with natural_keys functions declared.
class Comments(models.Model):
profile = models.ForeignKey('Profiles', models.DO_NOTHING)
book = models.ForeignKey(Books, models.DO_NOTHING)
date = models.DateTimeField()
text = models.TextField()
class Meta:
managed = False
db_table = 'comments'
class Profiles(models.Model):
alias = models.CharField(max_length=40)
mail = models.CharField(max_length=255)
mainimg = models.ForeignKey(Multimedia, models.DO_NOTHING)
birthdate = models.DateTimeField(blank=True, null=True)
country = models.CharField(max_length=30, blank=True, null=True)
password = models.CharField(max_length=255)
terms = models.IntegerField(blank=True, null=True)
device_token = models.CharField(max_length=500)
def natural_key(self):
return (self.pk, self.alias, self.country, self.mainimg)
class Meta:
managed = False
db_table = 'profiles'
class Multimedia(models.Model):
url = models.CharField(max_length=255)
title = models.CharField(max_length=100)
alt = models.CharField(max_length=150, blank=True, null=True)
description = models.CharField(max_length=150, blank=True, null=True)
mytype = models.CharField(max_length=20, blank=True, null=True)
extension = models.CharField(max_length=6, blank=True, null=True)
def natural_key(self):
return (self.pk, self.url)
class Meta:
managed = False
db_table = 'multimedia'
When I do a get comments query, I want a full response with the comment details, some book details, and profile details (including the picture). Everything goes fine except when I want the profile mainimg being serialized with natural keys.
The error response is
is not JSON serializable
when executing this:
def getcomments(request):
#Book get all comments - returns all comments on a book.
profilelogged = validtoken(request.META['HTTP_MYAUTH'])
if not profilelogged:
return HttpResponse('Unauthorized', status=401)
else:
index = request.GET.get('id', 0)
bookselected = Books.objects.filter(pk=index).first()
comments = list(Comments.objects.filter(book=bookselected).order_by('-date').all())
books_json = serializers.serialize('json', comments, use_natural_foreign_keys=True)
return HttpResponse(books_json, content_type='application/json')
Anyway I can get multimedia url on comment query on same response object serialized?
Thanks.
You are trying to convert ForeignKey object into JSON object which gives error as ForeignKey contains serialized data,
So you have to use safe parameter to convert your data into JSON.
return HttpResponse(books_json, content_type='application/json', safe=False)
if it doesn't work! Try this:
return HttpResponse(books_json, safe=False)
Otherwise you can always user JsonResponse as it is safer to user for propagation of JSON objects.
P.S:
Why your Profile ForeignKey in first Model is in quotes? Is it on purpose?
Thanks everyone.
I have reached what i want adding to the Profiles model natural_key function the Multimedia fields I want to use, insted of the full Multimedia model I do not need.
class Profiles(models.Model):
alias = models.CharField(max_length=40)
mail = models.CharField(max_length=255)
mainimg = models.ForeignKey(Multimedia, models.DO_NOTHING)
birthdate = models.DateTimeField(blank=True, null=True)
country = models.CharField(max_length=30, blank=True, null=True)
password = models.CharField(max_length=255)
terms = models.IntegerField(blank=True, null=True)
device_token = models.CharField(max_length=500)
def natural_key(self):
return (self.pk, self.alias, self.country, self.mainimg.pk, self.mainimg.url)
class Meta:
managed = False
db_table = 'profiles'
And now, the reponse is what I wanted.
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.