i am Trying to upload a shape file to postgres by django and geopandas. but it return this error:
(psycopg2.errors.DuplicateTable) relation "idx_Village_kcnT1Uu_geom" already exists
this is my model:
class ShapeFile(models.Model):
name = models.CharField(max_length=45)
description = models.TextField()
file = models.FileField(upload_to='user_shape_file')
date = models.DateTimeField(auto_now_add=True)
and i am using this signal to upload it:
#receiver(post_save, sender=ShapeFile)
def post_save_r(instance, **kwargs):
file = instance.file.path
file_format = os.path.basename(file).split('.')[-1]
file_name = os.path.basename(file).split('.')[0]
file_path = os.path.dirname(file)
name = instance.name
connection = 'postgresql://postgres:1234#localhost:5432/geoapp'
with zipfile.ZipFile(file, 'r') as opened_zip:
opened_zip.extractall(file_path)
shape_file = glob.glob(r'{}/**/*.shp'.format(file_path), recursive=True)[0]
gfr = gpd.read_file(shape_file)
epsg = 4326
engine_ = create_engine(connection)
gfr['geom'] = gfr['geometry'].apply(lambda x: WKTElement(x.wkt, srid=epsg))
gfr.to_sql(name, engine_, 'public', if_exists='replace', index=False,
dtype={'geom': Geometry('Geometry', srid=epsg)})
but it return this error:
Related
hi please help me i have to model i want after create and save store the result save to main store
like this
store.name == mainstore.name
store.the_rest_of_quantity==mainstore.quantity
i trying to use signals but i fail
class Store(models.Model):
CHOICES = (
('NUM','number'),
('M','meter'),
)
name = models.CharField(max_length=60)
quantity = models.PositiveSmallIntegerField (validators=[MinValueValidator(0)],default=0,blank=True,null=True)
date_of_add = models.DateTimeField(auto_now_add=True)
add_new_item = models.PositiveSmallIntegerField (validators=[MinValueValidator(0)],default=0,blank=True,null=True)
date_of_remove = models.DateTimeField(auto_now =True)
remove_old_item = models.PositiveSmallIntegerField (validators=[MinValueValidator(0)],default=0,blank=True,null=True)
the_rest_of_quantity = models.PositiveSmallIntegerField (validators=[MinValueValidator(0)],default=0,blank=True,null=True)
accept_stor = models.BooleanField(default = False)
classyfiyed = models.CharField(max_length=3,choices=CHOICES,blank=True,null=True)
def __str__(self):
return 'Device Name :( {0} ) Have Quantity({1}) '.format(self.name,self.quantity)
def save(self, *args, **kwargs):
try:
totla_sum = sum([self.quantity , self.add_new_item])
self.the_rest_of_quantity = int(totla_sum - self.remove_old_item)
except Expression as identifier:
'you add remove bigger than quantity'
return super().save(*args, **kwargs)
class MainStore(models.Model):
name = models.CharField(max_length=120)
quantity = models.PositiveIntegerField (null=True,default=0)
store = models.OneToOneField(Army,on_delete=models.CASCADE,related_name='Store',null=True)
i use signals for solve it
signals.py
#receiver(post_save,sender=Store)
def create_store(sender,instance,created,**kwargs):
if created:
MainStore.objects.get_or_create(Store=instance,name=instance.name,quantity=instance.the_rest_of_quantity)
else:
obj_store = MainStore.objects.filter(Store=instance)
update_store = obj_store.update(name=instance.name,quantity=instance.the_rest_of_quantity)
return update_store
apps.py
from django.apps import AppConfig
class StoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'Store'
def ready(self):
import Store.signals
init.py
default_app_config = "Store.apps.StoreConfig"
views.py
#api_view(["POST", "GET"])
#permission_classes([IsAuthenticated])
def add_order(request, pk):
print(request.user)
customer = md.Customer.objects.get(pk=pk)
if request.method == "POST":
description = request.data['description']
price = request.data['price']
order = md.Order.objects.create(customer=customer,
date_created=zt.now(),
description=description,
price=float(price),
customer_total_when_created=customer.total_owe[0]
)
try:
is_paid = request.data['is_paid']
if is_paid == "on":
who_paid = request.data['who_paid']
payment_method = request.data['payment_method']
who_took_money = request.user
if who_paid == customer.name:
order.who_paid = customer
elif who_paid == customer.parent:
order.who_paid = customer.parent
order.payment_method = payment_method
order.who_took_money = who_took_money
order.date_paid = zt.now()
customer.total = customer.total_owe
customer.save()
order.save()
except:
print("no payment succeed")
order_api = OrderSerializer(order)
return Response(order_api.data)
customer_api = CustomerSerializer(customer)
parent_api = CustomerSerializer(customer.parent)
context = {
"customer": customer_api.data,
"parent":parent_api.data,
"sample": [
{"description": "secreal", "price": "12.21", "is_paid": "on", "who_paid": "azra", "payment_method": "CARD"}
]
}
return Response(context)
models.py
class Order(models.Model):
payment_method_list = [("CASH","CASH"),("CARD","CARD")]
customer = models.ForeignKey(Customer,on_delete=models.CASCADE,null=True,blank=True,)
who_paid = models.ForeignKey(Customer,on_delete=models.CASCADE,null=True,blank=True, related_name='%(class)s_requests_created')
who_took_money = models.ForeignKey(User,on_delete=models.CASCADE,null=True,blank=True, related_name='who_took_money')
payment_method = models.CharField(choices=payment_method_list,max_length=4,default="CASH",blank=True,null=True)
date_paid = models.DateTimeField(blank=True,null=True)
date_created = models.DateTimeField(blank=True, null=True)
date_modified = models.DateTimeField(blank=True, null=True, auto_now=True)
is_paid = models.BooleanField(default=False, blank=True,null=True)
customer_total_when_paid = models.DecimalField(max_digits=5,decimal_places=2,null=True,blank=True)
customer_total_when_created = models.DecimalField(max_digits=5,decimal_places=2,null=True,blank=True)
description = models.TextField(blank=True,null=True)
price = models.DecimalField(max_digits=5,decimal_places=2,null=True,blank=True)
def __str__(self):
return self.description[0:12]
from django.db.models.signals import post_save
def update_total_owe_created_order(sender,instance,created,**kwargs):
if created:
order = instance
customer = order.customer
customer.total = customer.total_owe[0]
customer.save()
post_save.connect(update_total_owe_created_order,sender=Order)
def update_total_owe_updated_order(sender,instance,created,**kwargs):
if created==False:
order = instance
customer = order.customer
customer.total = customer.total_owe[0]
customer.save()
post_save.connect(update_total_owe_updated_order,sender=Order)
I have an app that you send your order to and it adds to database.
It only allows authenticated user to add order; so it shows who added that order. I can get the request.user with JWT authentication but can't save the model. When I send an API post request, I get that error. I could get the user with token but it gives me that error. I don't know why. I am using postman. I didn't get it normally but when I use postman it doesn't work.
ret = super().data
File "C:\Users\homet\Desktop\pharmacy\venv\lib\site-packages\rest_framework\serializers.py", line 253, in data
self._data = self.to_representation(self.instance)
File "C:\Users\homet\Desktop\pharmacy\venv\lib\site-packages\rest_framework\serializers.py", line 522, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "C:\Users\homet\Desktop\pharmacy\venv\lib\site-packages\rest_framework\serializers.py", line 522, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "C:\Users\homet\Desktop\pharmacy\venv\lib\site-packages\rest_framework\fields.py", line 1127, in to_representation
value = decimal.Decimal(str(value).strip())
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
The price in your Order model is a decimal field but you are casting the price from post request to float while creating the Order object.
price = models.DecimalField(max_digits=5,decimal_places=2,null=True,blank=True)
The price should be a Decimal object instead of float.
order = md.Order.objects.create(customer=customer,
date_created=zt.now(),
description=description,
price=Decimal(price),
customer_total_when_created=customer.total_owe[0]
)
customer_total_when_created should also be a Decimal instance but you haven't posted the Customer model which has total_owe.
Models.py:
class Enterprise(models.Model):
name = models.CharField(max_length = 100)
def __str__(self):
return f"{self.id}_{self.name}"
class Client(models.Model):
name = models.CharField(max_length = 100)
def __str__(self):
return f"{self.id}_{self.name}"
class Engagement(models.Model):
name = models.CharField(max_length = 100
def __str__(self):
return f"{self.id}_{self.name}"
class StockCount(models.Model):
name = models.CharField(max_length = 100)
def __str__(self):
return f"{self.name}"
class InventoryList(models.Model):
UploadedFile = models.FileField(_('Upload Inventory Listing'), upload_to = file_upload_location, validators=[validate_file_extension], max_length = 500)
enterprise = models.ForeignKey('Enterprise', on_delete=models.CASCADE, related_name = 'inventorylists')
client = models.ForeignKey('Client', on_delete=models.CASCADE, related_name = 'inventorylists')
engagement = models.ForeignKey('Engagement', on_delete=models.CASCADE, related_name = 'inventorylists')
stockcount = models.ForeignKey('StockCount', on_delete=models.CASCADE, related_name = 'inventorylists')
views.py:
def upload(request):
if request.method == 'POST':
form = InventoryListForm(request.POST, request.FILES)
if form.is_valid():
# file is saved
list = form.save(commit = False)
list.enterprise = Enterprise.objects.get(pk = 1)
list.client = Client.objects.get(pk = 1)
list.engagement = Engagement.objects.get(pk = 1)
list.stockcount = StockCount.objects.get(pk = 1)
list.save()
return HttpResponse(f'You have just made a post request - {list.id}')
else:
return render(request, "observe/upload.html", {"form": form})
else:
return render(request, "observe/upload.html", {"form": InventoryListForm()})
forms.py:
class InventoryListForm(ModelForm):
class Meta:
model = InventoryList
exclude = ['enterprise', 'client', 'engagement', 'stockcount']
def __init__(self, *args, **kwargs):
super(InventoryListForm, self).__init__(*args, **kwargs)
upload_to callable function:
def file_upload_location(instance, filename):
ext = filename.split('.')[-1]
# return f"{instance.enterprise}/{instance.client}/{instance.engagement}/{instance.stockcount}/{filename}"
# return f"{filename}"
FileType = '\\Inventory List'
name = str(filename)
path = os.path.join(str(instance.enterprise), str(instance.client), str(instance.engagement), str(instance.stockcount))
# return f"{path}/{filename}"
# return path
print(f"The path is {path}")
# return f"{path}/"
# return '{0}/{1}/{2}/{3}/{4}'.format(str(instance.enterprise), str(instance.client), str(instance.engagement), str(instance.stockcount), filename)
return os.path.join("%s" % str(instance.enterprise), "%s" % str(instance.client), "%s" % str(instance.engagement), "%s" % str(instance.stockcount), filename)
I have tried multiple variations of the callable functions (as can be seen above from the commented out portions of the function), but I still am getting this error. What is interesting is that when it is the first time I upload the file and submit the form, it works. However, the second time I try, it gives me this errno2 error.
The full traceback is as follows:
[Errno 2] No such file or directory: 'C:\Users\bilal\Desktop\Inventory Observation Mobile Responsive Web Application\Inventory-observation-mobile-responsive-web-application\inventoryobservation\files\1_RSM Canada\PK752_Nuvera Corp\FY 2021_Prospectus work\London Count\Monthly_Sales_Reporting_Template_Tool_Start.xlsm'
It appears as though my actual solutions works for some excel files and not for others, and based on further inspection of the excel files, there doesn't appear to be anything different about the excel files that I am able to successfully upload and the ones that I am not able to upload.
Try to Use this :-
UploadedFile = models.FileField(_('Upload Inventory Listing'), upload_to = 'file_upload_location', validators=[validate_file_extension], max_length = 500)
instead of :-
UploadedFile = models.FileField(_('Upload Inventory Listing'), upload_to = file_upload_location, validators=[validate_file_extension], max_length = 500)
What i've edited :- I have added comas on 'file_upload_location'.
Try to Use it .
i am generating thumbnail and i want to save image path to database when i upload from admin and api:
class Image(models.Model):
license_type = (
('Royalty-Free','Royalty-Free'),
('Rights-Managed','Rights-Managed')
)
image_number = models.CharField(default=random_image_number,max_length=12,unique=True)
title = models.CharField(default=random_image_number,max_length = 100)
image = models.ImageField(upload_to = 'image' , default = 'demo/demo.png')
thumbnail = models.ImageField(upload_to='thumbs')
category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.CASCADE)
shoot = models.ForeignKey(ImageShoot, on_delete=models.CASCADE, related_name='Image', null=True,blank=True)
image_keyword = models.TextField(max_length=1000)
credit = models.CharField(max_length=150, null=True)
location = models.CharField(max_length=100, null=True)
license_type = models.CharField(max_length=20,choices=license_type, default='')
uploaded_at = models.TimeField(auto_now_add=True)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
super(Image, self).save(*args, **kwargs)
if not self.make_thumbnail():
raise Exception('Could not create thumbnail - is the file type valid?')
def make_thumbnail(self):
fh = storage.open(self.image.path)
image = PILImage.open(fh)
image.thumbnail((400,400),PILImage.ANTIALIAS)
fh.close()
thumb_name, thumb_extension = os.path.splitext(self.image.path)
thumb_extension = thumb_extension.lower()
thumb_filename = thumb_name + '_thumb' + thumb_extension
temp_thumb = BytesIO()
image.save(temp_thumb, FTYPE)
temp_thumb.seek(0)
self.thumbnail.save(thumb_filename, ContentFile(temp_thumb.read()), save=True)
temp_thumb.close()
return True
this if we upload image from admin
admin.py:
#admin.register(Image)
class ImageAdmin(admin.ModelAdmin):
readonly_fields=['image_number','uploaded_at']
fields = ['title','image_number','shoot','category',
'image','image_keyword','thumbnail','credit','license_type','location','uploaded_at']
this is my views for api currently just uploading bulk image with out thumbnail how can i create thumbnail from this :
views.py:
class EventImageUploadView(APIView):
def post(self, request,category):
#title = forms.CharField(max_length = 100)
file = request.data['file']
data={
'image':file,
'category_id':category
}
EventsImage.objects.create(**data)
return JsonResponse(json.dumps({'message': "Uploaded"}), status=200, safe=False)
i get this error and its generating multiple images i dont know what causing recursion:
maximum recursion depth exceeded in comparison
backtrace:
File "/home/tboss/Desktop/environment/live/backend/venv/lib/python3.7/site-packages/PIL/TiffImagePlugin.py", line 319, in __init__
if isinstance(value, Fraction):
File "/home/tboss/Desktop/environment/live/backend/venv/lib/python3.7/abc.py", line 139, in __instancecheck__
return _abc_instancecheck(cls, instance)
RecursionError: maximum recursion depth exceeded in comparison
I have to upload a file from django views using the file path from the local system. I'm using models.create method to save the filepath. But the image is not getting uploaded into the media directory.
I have tried the Content and File from django core.utils but it deos not work
def createevent(request):
file_path = os.path.join(settings.FILES_DIR) # Getting the directory with files
f = []
for (dirpath, dirnames, filenames) in walk(file_path):
f.extend(filenames)
break
f.remove(".DS_Store")
img = random.choice(f) # Select a random file
pa = os.path.abspath(img) # absolute file path
# pa = (img,File(pa))
response = File(pa)
print(pa)
loc = "School Ground"
if request.method == 'POST':
get_dateof = request.POST.get('dateof')
get_nameof = request.POST.get('nameof')
get_descof = request.POST.get('descof')
new_report = Event.objects.create(
name=get_nameof,
description=get_descof,
location=loc,
timeoftheevent=get_dateof,
user=request.user,
image= pa
) #creating a db record
return HttpResponse('')
my models.py
class Event(models.Model):
name = models.CharField(max_length=100,blank=False)
description = models.CharField(max_length=100,blank=False)
location = models.CharField(max_length=100,blank=False)
timeoftheevent = models.CharField(max_length=100,blank=False)
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
image = models.FileField(blank=False)
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.user.username
You can try this.
#This would return a path for your saved images using path name/filename you can always tweak this
def user_directory_path(instance,filename):
return '{0}/{1}'.format(instance.name, filename)
class Event(models.Model):
name = models.CharField(max_length=100,blank=False)
description = models.CharField(max_length=100,blank=False)
location = models.CharField(max_length=100,blank=False)
timeoftheevent = models.CharField(max_length=100,blank=False)
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
image = models.FileField(upload_to = user_directory_path, blank=False)
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.user.username
then in your views.py and I'm assuming myfile is the name of the file field in your html form
if request.method == 'POST' and and request.FILES['myfile']:
get_dateof = request.POST.get('dateof')
get_nameof = request.POST.get('nameof')
get_descof = request.POST.get('descof')
myfile = request.FILES['myfile']
#creating a db record............
return HttpResponse('')
you can have a look at this article if it helps