django, Changing field values ​in model before saving to database - python

class PlayerList(models.Model):
name = models.CharField(max_length=300)
position = models.CharField(max_length=200)
h_code = models.ForeignKey(HList, related_name="h_code", on_delete=models.CASCADE)
d_code = models.CharField(primary_key=True, max_length = 200, editable=False)
Serializers.py
class PlayerSerializer(serializers.ModelSerializer):
class Meta:
fields = ["name", "position", "h_code", "d_code"]
model = PlayerList
view.py
class PostPlayer(generics.ListCreateAPIView):
queryset = PlayerList.objects.all().order_by('-d_code')
serializer_class = PlayerListSerializer
def get(self, request, *args, **kwargs):
d_code = request.data.get('h_code') + 'test'
print(d_code)
print(d_code) :
h000001test
When entering a value through api, I want to implement that the entered value is changed to another value in view.py and saved in the db.
I want to save d_code processed by def get in db.
I don't know what to do. Can you please let me know?
There is no answer, so I will post again.

You can override your views perform_create() method, as stated in the docs (you might have to scroll a little, its in the Save and deletion hooks section:
class PostPlayer(generics.ListCreateAPIView):
queryset = PlayerList.objects.all().order_by('-d_code')
serializer_class = PlayerListSerializer
def get(self, request, *args, **kwargs):
d_code = request.data.get('h_code') + 'test'
print(d_code)
...
...
def perform_create(self, serializer):
d_code = request.data.get('h_code') + 'test'
#do something with d_code
new_code = d_code + 'someSampleValue'
serializer.save(d_code=new_code)

Related

How to handle PUT request in Django RestFramework

I am trying to update my data in 'VoterList' model by using PUT api, but i don't know which function should i use in my 'views.py' file to handle the coming PUT request because in PUT api, we use parameters from URL to pick the relevent entry from model for updation and then update it by using data received from PUT api.
model.py
class VoterList(models.Model):
# id = models.IntegerField(auto_created= True, primary_key=True)
name = models.CharField( max_length=20)
email = models.EmailField()
mobile = models.IntegerField()
city = models.CharField( max_length=20)
type = models.CharField(max_length=20)
def __str__(self):
return self.name
serializers.py
class FillVoterListSerializers(serializers.HyperlinkedModelSerializer):
class Meta:
model = VoterList
fields = ('id','name', 'email', 'mobile', 'city', 'type')
def update(self, instance, validated_data):
instance.name = validated_data.pop("name", instance.name)
instance.email = validated_data.pop("email", instance.email)
instance.save()
return instance
I will manage the code for PUT in serializers by myself.
views.py
class UpdateVoter(APIView):
serializer_class = FillVoterListSerializers
permission_classes = (AllowAny,)
def post(self, request,*args,**kwargs):
isDataExist = VoterList.objects.get(id=request.data.get('id'))
if not isDataExist:
return Response({"message":"No Voter exist with this id."})
else:
isDataUpdated = self.serializer_class(isDataExist, request.data, partial=True)
if isDataUpdated.is_valid():
isDataUpdated.save()
return Response({"message": "Voter updated."})
else:
return Response({"message": "All fields are Mandatory."})
urls.py
urlpatterns = [
url('api/updateVoter/(?P<id>[0-9]+)/$', UpdateVoter.as_view(), name= "updateVoter")]
So what code should i write in my view.py to handle the PUT request.
Note: I want to tell you that i am preparing api for mobile applications, so please respond accordingly.
Any help is appreciated.
You can use the put() function in your view similar to the post() which you've used
def put(self, request, pk, format=None):
# Your code here
Refer the DRF docs : https://www.django-rest-framework.org/tutorial/3-class-based-views/

Django ModelForm Foreign Key Dropdown passes __str__ but stores fkey_id

There are two models and I would like to store a column field from one model to another model.
models.py:
class Company(models.Model):
name = ...
def __str__(self):
return self.name
class Rate(models.Model):
company = models.ForeignKey(Company)
currency = ...
name = ...
def __str__(self):
return self.name + " " + self.currency
class Client(models.Model):
name = ...
currency = ....
company = models.ForeignKey(Company)
base_rate = models.ForeignKey(Rate)
def __str__(self):
return self.name
forms.py:
class ClientForm(forms.ModelForm):
class Meta:
model = Client
fields = (
"name",
"company",
"base_rate", )
views.py:
class ClientCreateView(FormView):
template_name = "client/new_package.html"
form_class = ClientForm
success_url = reverse_lazy("home")
def form_valid(self, form):
detail = form.save(commit=False)
base_rate_id = form.cleaned_data['base_rate']
detail.currency = Rate.objects.values_list("currency", flat=True).filter(base_rate_id=base_rate_id)
detail.save()
if detail is not None:
return redirect('display_package' , detail.id)
else:
return super(ClientCreateView, self).form_valid(form)
Basically, I want selected currency value to be saved in my client model from Rate model. Any help will be appreciated as from cleaned data I am getting str returned value instead of selected option id value (i.e. str from client model ).
I'm not really sure why you want that. But more to the point, why you are doing it with filter and values_list? You need to get the value and store it.
detail = form.save(commit=False)
detail.currency = form.cleaned_data['base_rate'].currency
detail.save()

Send current user when model is saved in Django

I have a problem after a bigger upgrade from Django 1.7 to 1.10 and to Django Rest Framework 3.5.4. The problem is when I try to access the endopint /claims it returns a 500 Error with the text: IntegrityError at /claims (1048, "Column 'user_id' cannot be null")
In urls.py that route is added like this:
url('^claims', v.ClaimList.as_view(), name='claim_list')
And the relevant part from the views.py file is:
class ClaimSerializer(serializers.ModelSerializer):
company_name = s.ReadOnlyField(source="ad.company.name")
company_address = s.ReadOnlyField(source="ad.company.address")
ad_thumbnail = ThumbnailField(source="ad.picture", size="200x200", read_only=True)
class ClaimSerializerDeep(ClaimSerializer):
class Meta:
exclude = ('user',)
model = m.Claim
depth = 2
class ClaimSerializerFlat(ClaimSerializer):
class Meta:
exclude = ('user',)
model = m.Claim
#permission_classes((IsAuthenticated,))
class ClaimList(Limitable, generics.ListCreateAPIView):
model = m.Claim
def get_queryset(self):
tab = self.request.GET.get("tab", "active")
q = m.Claim.objects.filter(user=self.request.user.pk)
return self.limit(q)
def pre_save(self, obj):
obj.user = self.request.user
def get_serializer_class(self):
if self.request.method == "POST":
return ClaimSerializerFlat
else:
return ClaimSerializerDeep
And the claim model is:
class Claim(models.Model):
ad = models.ForeignKey("Ad")
user = models.ForeignKey(settings.AUTH_USER_MODEL)
created = models.DateTimeField(auto_now_add=True)
redeemed = models.BooleanField(default=False)
def save(self, *args, **kwargs):
increase_claimed = False
super(Claim, self).save(*args, **kwargs) # here is where it crashes
Do you have any ideas what could cause the problem?
user = models.ForeignKey(settings.AUTH_USER_MODEL)
Your model requires that a user gets set, but obviously it isn't (anymore).
The question is why?
Looking at your code this seens to be the relevant line:
def pre_save(self, obj):
obj.user = self.request.user
Assuming you also upgraded DRF it is quite plausible that this doesn't work anymore: http://www.django-rest-framework.org/topics/3.0-announcement/#changes-to-prepost-save-hooks
So you have to adapt your code and use perform_create as explained in the docs.

Django ModelForm - Performance issue

I am using ModelForm to allow multiple rows edit at the same time. It is a very simple form that has series of yes_no columns. The model looks like:
models.py
class Yn(models.Model):
yn_id = models.IntegerField(primary_key=True)
description = models.CharField(max_length=30)
def __str__(self):
return ' '.join([
self.description,
])
class Meta:
managed = False
db_table = 'yn'
class Invoice(models.Model):
description = models.CharField(max_length=50)
invoice_date = models.DateTimeField()
...
invoice_sent_yn = models.ForeignKey('Yn', models.DO_NOTHING, db_column='invoice_sent_yn', related_name="invoice_sent_yn")
confirm_receipt_yn = models.ForeignKey('Yn', models.DO_NOTHING, db_column='confirm_receipt_yn', related_name="confirm_receipt_yn")
paid_yn = models.ForeignKey('Yn', models.DO_NOTHING, db_column='paid_yn', related_name="paid_yn")
forms.py
class InvoiceGridEdit(ModelForm):
model = Invoice
fields = ['description','invoice_date','invoice_sent_yn', 'confirm_receipt_yn', 'paid_yn']
def __init__(self, *args, **kwargs):
super(JurisGridEditForm, self).__init__(*args, **kwargs)
...
...
InvoiceFormSet = modelformset_factory(models.Invoice, form=InvoiceGridEdit)
views.py
class InvoiceUpdateGrid(CreateView):
template_name = "/invoice_update_grid.html"
model = Invoice
form_class = InvoviceViewEditForm
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return redirect("account_login")
def get(self, request, *args, *kwargs):
self.object = None
customer_number = self.request.GET.get('customer_number')
invoice_form = InvoiceFormSet(queryset=Invoice.objects.filter(customer_number = customer_number)
return self.render_to_response(self.get_context_data(invoice_form=invoice_form))
def post(self, request, *args, **kwargs):
self.object = None
invoice_form = InvoiceFormSet(self.request.POST)
if (invoice_form.is_valid()):
return self.form_valid(invoice_form)
else:
return self.form_invalid(invoice_form)
def form_valid(self, invoice_form):
...
...
invoice_form.save()
return redirect("customer_list")
def form_invalid(self, invoice_form):
return self.render_to_response(self.get_context_data(invoice_form=invoice_form))
The forms works fine, get and post works, except, it takes a while (~30 sec) to retrieve and update. Using django-debug-toolbar, it looks like the yn columns retrieve separately for each column for each row (~2k rows). The Yn table only has 3 rows -1 - Unknown, 0 - No, 1 - Yes.
I tried to search for work around to stop the craziness of Django hitting the DB 900 times per retrieval. I found something about caching but I have no idea how to do it.
Thanks in advance.

Using super() on Python2.x classes in Django

My models.py looks like this :
class Prescription(models.Model):
date_prescribed = models.DateTimeField()
doctor = models.ForeignKey(Doctor)
pharmacy = models.ForeignKey(Pharmacy)
class Doctor(models.Model):
name = models.CharField(max_length=150)
age = models.PositiveSmallIntegerField()
class Pharmacy(models.Model):
name = models.CharField(max_length=150)
status = models.CharField()
In my views,I want the count of prescriptions grouped by month.I am using Django rest framework.My views.py is as follows:
class PrescriptionTrendListView(generics.ListAPIView):
queryset = Prescription.objects.all()
serializer_class = LineGraphSerializer
def get_queryset(self):
end_date = timezone.now()
start_date = end_date - relativedelta(months=6)
truncate_date = connection.ops.date_trunc_sql('month', 'date_prescribed')
qs = super().get_queryset.extra(select={'month': truncate_date})
return qs.filter(date_prescribed__range=(start_date, end_date)
).annotate(pk_count=Count('pk')).order_by('month')
def get(self, request, *args, **kwargs):
graph_data = self.get_queryset().values('pk_count', 'month')
serializer = self.get_serializer(data=graph_data, many=True)
return Response(serializer.data)
However,the error I get when I run this is :
I am assuming this has something to do with new style classes in Python 3.0.But that is all i know.Any help on how to fix this on 2.x ?
Your super() call in get_queryset is missing the type and self arguments.
class PrescriptionTrendListView(generics.ListAPIView):
queryset = Prescription.objects.all()
serializer_class = LineGraphSerializer
def get_queryset(self):
end_date = timezone.now()
start_date = end_date - relativedelta(months=6)
truncate_date = connection.ops.date_trunc_sql('month', 'date_prescribed')
# Bug was here
qs = super(PrescriptionTrendListView, self).get_queryset.extra(select={'month': truncate_date})
return qs.filter(date_prescribed__range=(start_date, end_date)
).annotate(pk_count=Count('pk')).order_by('month')
def get(self, request, *args, **kwargs):
graph_data = self.get_queryset().values('pk_count', 'month')
serializer = self.get_serializer(data=graph_data, many=True)
return Response(serializer.data)
https://docs.python.org/2/library/functions.html#super
In Python 2, syntax for super is this:
class MyClass(Base):
def func(self, *args, **kwargs):
super(MyClass, self).func(*args, **kwargs)
This is really stupid but I suspect it's because you're referencing Doctor and Pharmacy before you declare them. Try this instead:
class Doctor(models.Model):
name = models.CharField(max_length=150)
age = models.PositiveSmallIntegerField()
class Pharmacy(models.Model):
name = models.CharField(max_length=150)
status = models.CharField()
class Prescription(models.Model):
date_prescribed = models.DateTimeField()
doctor = models.ForeignKey(Doctor)
pharmacy = models.ForeignKey(Pharmacy)
I looked at some of my old Django on Python 2.x and your models look fine, just that seems to be an issue maybe(?)

Categories