I have a value in a database on phpmyadmin and I want to change this value by a new calculated value. The function save() doesn't work and the error tells me that it's because it's an interger. I don't know how to solve this problem.
def DeleteBulle (request, id_bulle):
#suppression de la bulle par l'id
id_bulle
param = Bulles.objects.get(pk=id_bulle)
#param.delete()
#adaptation du champ "nombre de bulle" dans la table "site"
site=param.id_site
print('site', site)
compte=Bulles.objects.filter(id_site=site).count()
print('nombre bulle avec site identique', compte)
nbrbulle=Bulles.objects.get(pk=id_bulle).id_site.nombre_bulles
nbrbulle=compte
nbrbulle.save()
#réussite
print("Bulle supprimée")
return redirect('api_bulles_frontend')
Models :
class Site(models.Model):
id_site = models.AutoField(
db_column="Id_site", primary_key=True
) # Field name made lowercase.
nom = models.CharField(
db_column="Nom", max_length=100
) # Field name made lowercase.
vitesse_b = models.FloatField(db_column="Vitesse_b") # Field name made lowercase.
vitesse_c = models.FloatField(db_column="Vitesse_c") # Field name made lowercase.
ecart_type_b = models.FloatField(
db_column="Ecart_type_b"
) # Field name made lowercase.
ecart_type_c = models.FloatField(
db_column="Ecart_type_c"
) # Field name made lowercase.
type_site = models.CharField(
db_column="Type_site", max_length=20
) # Field name made lowercase.
longitude = models.FloatField(db_column="Longitude") # Field name made lowercase.
latitude = models.FloatField(db_column="Latitude") # Field name made lowercase.
nombre_bulles = models.IntegerField(db_column="Nombre_bulles")
date_vidange = models.DateField(
db_column="Date_vidange"
) # Field name made lowercase.
#trajet = models.ManyToManyField(Trajet, related_name='site_moi')
class Meta:
db_table = "site"
class Bulles(models.Model):
id_bulle = models.AutoField(primary_key=True)
num_bulle = models.CharField(max_length=20)
type_bulle = models.CharField(max_length=20)
colories = models.CharField(max_length=20)
latitude = models.FloatField()
longitude = models.FloatField()
date_vidange = models.DateField(
db_column="date_vidange"
)
id_depot = models.ForeignKey(
"Depot", on_delete=models.CASCADE, db_column="id_depot"
)
id_site = models.ForeignKey(
"Site",related_name='bul', on_delete=models.CASCADE, db_column="Id_site"
)
class Meta:
db_table = "bulles"
I don't know how to solve this problem. Thank you for your help.
You should save the site object with the updated item, not update the nombre_bulles, which is just an integer:
from django.shortcuts import get_object_or_404
from django.views.decorators.http import require_http_methods
#require_http_methods(['POST', 'DELETE'])
def DeleteBulle(request, id_bulle):
bulle = get_object_or_404(Bulles, pk=id_bulle)
site = param.id_site
bulle.delete()
site.nombre_bulles = site.bul.count()
site.save(update_fields=['nombre_bulles'])
return redirect('api_bulles_frontend')
Note: normally a Django model is given a singular name, so Bulles instead of Bulle.
Note: Normally one does not add a prefix id_… to a ForeignKey field, since Django
will automatically add a "twin" field with an …_id suffix. Therefore it should
be site, instead of id_site.
Note: It is often better to use get_object_or_404(…) [Django-doc],
then to use .get(…) [Django-doc] directly. In case the object does not exists,
for example because the user altered the URL themselves, the get_object_or_404(…) will result in returning a HTTP 404 Not Found response, whereas using
.get(…) will result in a HTTP 500 Server Error.
Note: One can use the #require_POST decorator [Django-doc]
to restrict the view to only be accessible for a POST request.
Related
I'm trying to connect to SQLite table using a raw sql query but unsuccessfully.
Here is my model:
class CsqAgentReport(models.Model):
nodeid_sessionid_sequenceno = models.TextField(db_column='NodeID-SessionID-SequenceNo', blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters.
callstarttime = models.TextField(db_column='CallStartTime', blank=True, null=True) # Field name made lowercase.
callendtime = models.TextField(db_column='CallEndTime', blank=True, null=True) # Field name made lowercase.
contactdisposition = models.IntegerField(db_column='ContactDisposition', blank=True, null=True) # Field name made lowercase.
originatordn_callingnumber_field = models.IntegerField(db_column='OriginatorDN(CallingNumber)', blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters. Field renamed because it ended with '_'.
destinationdn = models.IntegerField(db_column='DestinationDN', blank=True, null=True) # Field name made lowercase.
callednumber = models.IntegerField(db_column='CalledNumber', blank=True, null=True) # Field name made lowercase.
pivotoriginatordn = models.TextField(db_column='PivotOriginatorDN', blank=True, null=True) # Field name made lowercase.
pivotcallednumber = models.TextField(db_column='PivotCalledNumber', blank=True, null=True) # Field name made lowercase.
csqnames = models.TextField(db_column='CSQNames', blank=True, null=True) # Field name made lowercase.
queuetime = models.TextField(db_column='QueueTime', blank=True, null=True) # Field name made lowercase.
agentname = models.TextField(db_column='AgentName', blank=True, null=True) # Field name made lowercase.
ringtime = models.TextField(db_column='RingTime', blank=True, null=True) # Field name made lowercase.
talktime = models.TextField(db_column='TalkTime', blank=True, null=True) # Field name made lowercase.
worktime = models.TextField(db_column='WorkTime', blank=True, null=True) # Field name made lowercase.
nomcsq = models.TextField(db_column='NomCSQ', blank=True, null=True) # Field name made lowercase.
idunique = models.IntegerField(db_column='IDUnique', blank=True, null=True) # Field name made lowercase.
originatordnhandeled = models.IntegerField(db_column='OriginatorDNHANDELED', blank=True, null=True) # Field name made lowercase.
originatordnnothandeled = models.IntegerField(db_column='OriginatorDNNOTHANDELED', blank=True, null=True) # Field name made lowercase.
outboundmissedcall = models.TextField(db_column='OutboundMISSEDcall', blank=True, null=True) # Field name made lowercase.
missedcallshandeledy_n = models.IntegerField(db_column='MISSEDCALLSHANDELEDY-N', blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters.
class Meta:
managed = False
db_table = 'CSQ Agent Report'
Here is the view I created:
def csq_detail_view(request):
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM 'CsqAgentReport'")
obj = cursor.fetchone()
context = {
'object': obj
}
return render(request,"CSQ/detail.html",{context})
and here is the html:
{% extends 'base.html' %}
{% block content %}
<ul>
{% for var_nbr_app_repond in object %}
<li> {{var_nbr_app_repond}} </li>
{% endfor %}
{% endblock %}
The error message is the following :
Request Method: GET
Request URL: http://127.0.0.1:8000/CSQ_Detail/
Django Version: 3.0.5
Exception Type: OperationalError
Exception Value:
no such table: CsqAgentReport.
no such table: CsqAgentReport.
the error is self explanatory, did you create the database ? is there sqlite3 file in your project root folder ?
run those commands :
(venv) python manage.py makemigrations
to create a migration for your model, and
(venv) python manage.py migrate
to create associated table in your database.
Update
you've made a mistake, refer to https://docs.djangoproject.com/en/3.1/ref/models/options/#db-table on how to rename/override table name using db_table in Meta class. it should be:
class CsqAgentReport(models.Model):
[..]
class Meta:
managed = False
db_table = 'csq_agent_report' # HERE snake lower-cased strings
Update 2
since you've renamed db_table to csq_agent_report instead the default one CsqAgentReport you should update the sql statement to
cursor.execute("SELECT * FROM 'csq_agent_report'")
in return statement you don't need to wrap context in dictionary {..} because it's already.
change those 2 lines:
def csq_detail_view(request):
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM 'CsqAgentReport'") # HERE
obj = cursor.fetchone()
context = {
'object': obj
}
return render(request,"CSQ/detail.html",{context}) # HERE
to
def csq_detail_view(request):
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM 'csq_agent_report'") # HERE
obj = cursor.fetchone()
context = {
'object': obj
}
return render(request,"CSQ/detail.html", context) # HERE
Update 3
you have to change managed to True instead Flase
refer to https://docs.djangoproject.com/en/3.1/ref/models/options/#managed
class CsqAgentReport(models.Model):
[..]
class Meta:
managed = True # Here
db_table = 'csq_agent_report' # HERE snake lowercased strings
and don't forget to rerun migration commands.
I have a relationship as Follows
class Tblrfqvendor(models.Model):
"""RFQ Master for Vendors
This typical represents the same RFQ sent to different people
"""
ven_rfqid = models.ForeignKey(Tblrfqitem, db_column='RFQID', on_delete=models.DO_NOTHING, primary_key=True,related_name="venrfq") # Field name made lowercase.
lineitem = models.IntegerField(db_column='LineItem') # Field name made lowercase.
vendorid = models.CharField(db_column='VendorID', max_length=10) # Field name made lowercase.
class Meta:
managed = False
db_table = 'tblRFQVendor'
constraints = [constraints.UniqueConstraint(fields=['ven_rfqid', 'lineitem', 'vendorid'], name='unique_vendor_rfq')]
ordering = ['-lastrevdate']
class Tblrfqitem(models.Model):
"""A Line Item for a particular Master Vendor RFQ
"""
item_rfqid = models.ForeignKey(Tblrfqmaster, db_column='RFQID', on_delete=models.DO_NOTHING, primary_key=True,related_name='items') # Field name made lowercase.
lineitem = models.IntegerField(db_column='LineItem')
itemid = models.CharField(db_column='ItemID', max_length=100, blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'tblRFQItem'
constraints = [constraints.UniqueConstraint(fields=['item_rfqid','lineitem'], name='unique_item')]
How can I get it so that I can serialize on both lineitem and ven_rfid instead of just the single primary key?
This is what i got in the models
class SFE(models.Model):
snpid = models.ForeignKey(Snps, models.DO_NOTHING, db_column='SNPID', primary_key=True) # Field name made lowercase.
elementid = models.ForeignKey(Functionalelement, models.DO_NOTHING, db_column='ElementID') # Field name made lowercase.
celllineid = models.ForeignKey(Celllines, models.DO_NOTHING, db_column='CELLLINEID') # Field name made lowercase.
countexperiments = models.PositiveIntegerField(db_column='countExperiments') # Field name made lowercase.
filetype = models.CharField(db_column='fileType', max_length=10) # Field name made lowercase.
class Meta:
managed = False
db_table = 'SNPs_FunctionalElement'
unique_together = (('snpid', 'elementid', 'celllineid', 'filetype'),)
def __str__(self):
return str(str(self.snpid) + str(self.elementid) + str(self.celllineid) + str(self.filetype))
class Functionalelement(models.Model):
elementid = models.AutoField(db_column='ElementID', primary_key=True) # Field name made lowercase.
name = models.CharField(unique=True, max_length=55)
class Meta:
managed = False
db_table = 'FunctionalElement'
def __str__(self):
return str(self.elementid)
class Snps(models.Model):
snpid = models.AutoField(db_column='SNPID', primary_key=True) # Field name made lowercase.
rsid = models.CharField(unique=True, max_length=20)
chrom = models.CharField(max_length=5)
pos = models.PositiveIntegerField()
ref = models.CharField(max_length=1)
alt = models.CharField(max_length=1)
maf1000genomes = models.FloatField(blank=True, null=True)
maftopmed = models.FloatField(db_column='mafTOPMed', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'SNPs'
def __str__(self):
return str(self.snpid)
Now i want to join FunctionalElement with SFE in order to retrieve the field FunctionalElement.name given a specific SFE.snpid.
I tried with SFE.objects.select_related('elementid__name') but i know it's wrong and i can't understand how to work with django ORM
To get a simple object you need to do: a = SFE.objects.get(snpid=THE_SPECIFICSNPID) later you can access to all the related objects, for example: a.elementid.name will return what you want.
The Django ORM take retrieve the object for you, that is because "lazzy loading". That means that if you need a related object later Django will get it for you, of course, it need to do another query and to avoid that you need to call the method select_related
Summarizing:
To get the name you can do:
name = SFE.objects.get(snpid=THE_SPECIFICSNPID).select_related('elementid').elementid.name
It should works
Djago rest API is not filtering on fields that were created by MariaDB view.
selected
cnrbillable
Works fine for native db fields as:
swcustomerid
swparentid
When I'm filtering on selected or cnrbillable then django is ignoring my filtering request. Can django filter on fields "created" by MariaDB view?
Django model:
class VwMrsCustomers(models.Model):
swcustomerid = models.IntegerField(db_column='SWCUSTOMERID', primary_key=True) # Field name made lowercase.
swname = models.CharField(db_column='SWNAME', max_length=70, blank=True, null=True) # Field name made lowercase.
swparentid = models.IntegerField(db_column='SWPARENTID', blank=True, null=True) # Field name made lowercase.
selected = models.IntegerField(db_column='SELECTED', blank=True, null=True) # Field name made lowercase.
cnrbillable = models.IntegerField(db_column='CNRBILLABLE', default=1) # Field name made lowercase.
us = models.SmallIntegerField(db_column='US', default=0) # Field name made lowercase.
class Meta:
db_table = 'vw_mrs_customers'
MariaDB view:
CREATE
DEFINER = app_mrs_reporting
VIEW vw_mrs_customers
AS
(
SELECT
mrs_customers.SWCUSTOMERID,
mrs_customers.SWNAME,
mrs_customers.SWPARENTID,
mrs_customer_settings.US,
CASE when mrs_customer_settings.SWCUSTOMERID is null then 0 else 1 end AS SELECTED,
CASE when mrs_customer_settings.SWCUSTOMERID is not null then mrs_customer_settings.CNRBILLABLE else 0 end AS CNRBILLABLE
FROM
mrs_customers
LEFT JOIN
mrs_customer_settings ON (mrs_customers.SWCUSTOMERID = mrs_customer_settings.SWCUSTOMERID)
)
Django view:
class CustomerSettingsList(generics.ListAPIView):
queryset = VwMrsCustomers.objects.all()
serializer_class = CustomerSerializerSettings
filter_backends = (django_filters.rest_framework.DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter,)
filter_class = VwMrsCustomersFilter
filter_fields = ('swcustomerid', 'swname', 'swparentid', 'cnrbillable', 'selected')
search_fields = ('swcustomerid', 'swname', 'swparentid', 'cnrbillable', 'selected')
ordering_fields = ('swcustomerid', 'swname', 'swparentid', 'cnrbillable', 'selected')
Here is my code:
models.py
class TblUser(models.Model):
uid = models.IntegerField(primary_key=True, db_column='ID') # Field name made lowercase.
username = models.CharField(max_length=3072, db_column='UserName', blank=True) # Field name made lowercase.
password = models.CharField(max_length=3072, db_column='PassWord', blank=True) # Field name made lowercase.
datesstart = models.DateTimeField(null=True, db_column='datesStart', blank=True) # Field name made lowercase.
datesend = models.DateTimeField(null=True, db_column='datesEnd', blank=True) # Field name made lowercase.
num = models.IntegerField(null=True, db_column='Num', blank=True) # Field name made lowercase.
power = models.IntegerField(null=True, db_column='Power', blank=True) # Field name made lowercase.
email = models.CharField(max_length=12288, blank=True)
class Meta:
db_table = u'tbl_user'
def __unicode__(self):
return '%d--%s--%d'%(self.uid,self.username,self.power)
views.py
from app.models import TblUser
def appendUser(self,name,pwd):
#I add a new user,and the primary_key:uid is autoincrement in datebase
user = TblUser.objects.create(username=name,password=pwd)
print user.uid#None
When I call the appendUser(),it will insert a new record into datebase, and the user(TblUser's instance) only have two valid fields(username,password), the other is empty.
How can I get the user.uid because I want handle other things by using it?
You should use an AutoField not an IntegerField for the pk: https://docs.djangoproject.com/en/1.4/ref/models/fields/#autofield
You might want to read this post, different approach using ModelForm:
Get Primary Key after Saving a ModelForm in Django