How to set daily update limit for django model(db)?

160 views
Skip to first unread message

Shailesh Yadav

unread,
Jan 22, 2021, 10:58:32 AM1/22/21
to Django users
Hi I'm having one Django web app where My model A contains 1000 records and I want to set a daily update limit for example if users are updating that data by uploading a .xls file then it should count how many records updated and once it is more than 500 then the user should get an Error message.

Can Anyone help me how to implement this (Is there a SQLite parameter where so that I can define value in settings.py)

below is my upload code. (Here I have tried to store the count value and comparing it with half of the record but again how to reset it after 12 hrs?)


def CTA_upload(request):
try:
if request.method == 'POST':
movie_resource = CTAResource()
##we will get data in movie_resources####
dataset = Dataset()
new_movie = request.FILES['file']
if not new_movie.name.endswith('xls'):
messages.info(request, 'Sorry Wrong File Format.Please Upload valid format')
return render(request, 'apple/uploadinfosys.html')
messages.info(request, 'Uploading Data Line by Line...')
imported_data = dataset.load(new_movie.read(), format='xls')
count = 1
for data in imported_data:
value = CTA(
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6],
data[7],
data[8],
)
count = count + 1
value.save()
# messages.info(request, count)
# time.sleep(1)
messages.info(request, 'File Uploaded Successfully...')

except:
messages.info(request,
'Same Email ID has been observed more than once.Except that other records has been added../nPlease Make sure Email field should be unique.')

return render(request, 'apple/cta.html')

Gabriel Araya Garcia

unread,
Jan 22, 2021, 12:15:54 PM1/22/21
to django...@googlegroups.com
Previous, you can use:
howmany = mytable.count()

Also you can use xlrd library, for instance:
import xlrd
 
#open excel file
documento = xlrd.open_workbook("ejemplo.xls")
 
#We can save each one of the spreadsheet separately
libros = documento.sheet_by_index(0)
peliculas = documento.sheet_by_index(1)
  
On the other hand, the model has not contains records, it's only an structure definition of a table


Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/95a6c5e9-ad98-4036-bcbb-109562a7df06n%40googlegroups.com.

Shailesh Yadav

unread,
Jan 22, 2021, 12:39:35 PM1/22/21
to Django users
Thanks! but Can you elaborate more? 
howmany = mytable.count() = Here How it'll work and how to reset the value in 1 day. how I can use it in my code.


import xlrd   = 

I was trying something like this
def CTA_upload(request):
i = 1
count = (1,)
print('Counter value at starting :', len(count))
allcta = CTA.objecta.all()
allcta7 = len(tuple(allcta)) // 2
print('Tuple Check:', allcta7)

try:
if request.method == 'POST':
movie_resource = CTAResource()
##we will get data in movie_resources####
dataset = Dataset()
new_movie = request.FILES['file']
if not new_movie.name.endswith('xls'):
messages.info(request, 'Sorry Wrong File Format.Please Upload valid format')
return render(request, 'apple/uploadcts.html')
messages.info(request, 'Processing...')

imported_data = dataset.load(new_movie.read(), format='xls')
for data in imported_data:
value = CTA(
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6],
data[7],
data[8],
)
if len(count) <= allcta7:
value.save()
i+= 1
count = append(i)
else:
messages.info(request, 'Sorry You have uploaded more data than specified limit...')
count.clear()

except:
messages.info(request,'Same Email ID has been observed more than once.Except that other records has been added../nPlease Make sure Email field should be unique.')
print("Problem hai kuch to")

return render(request,'app/uploadcts.html')

Gabriel Araya Garcia

unread,
Jan 22, 2021, 3:33:51 PM1/22/21
to Django users

Jaimik Patel

unread,
Jan 25, 2021, 10:53:50 AM1/25/21
to Django users
Hi Shailesh,

you can define the function with the use of DateTime module, which changes the value of the variable daily for the count.

and for excel, you can get the number of lines in the file, and you can implement a validator as well. 
https://stackoverflow.com/questions/13377793/is-it-possible-to-get-an-excel-documents-row-count-without-loading-the-entire-d

Shailesh Yadav

unread,
Feb 4, 2021, 8:42:44 PM2/4/21
to Django users
Hi Jaimikp,

I have checked it but no luck. Can we make changes in the resource.py file?
check the count and store that count into a variable and the next day the count value should get reset.
I have checked before_import but not able to implement and also don't know if implementing this at resource.py level is good or at views.py logic code

Please find the below answer.

Chetan Ganji

unread,
Feb 13, 2021, 1:37:49 PM2/13/21
to django...@googlegroups.com
Hi Shailesh,

I am assuming you have created_at and updated_at fields (these or similar) on the model.
You can write a query like below to get the count of no of rows updated that day using below code.
Remember to update the filters of the queryset as required e.g. user.


from django.utils import timezone

current_date = timezone.now().date()

edited_count = Model.objects.filter(updated_at__date=current_date).count()


Reference :
https://docs.djangoproject.com/en/3.1/ref/models/querysets/#date

I hope it helps you.

Cheers! 


Regards,
Chetan Ganji
+91-900-483-4183


Reply all
Reply to author
Forward
0 new messages