Issue
Hi I am new to Django and due to time constraints i am making a personal_project on the go.I have been able to set upa login page and some forms to fetch the data going through videos and tutorial. The issue i am facing now is that for a location model.If i insert same data multiple times it is creating a new row in My DB. What would be the correct way to prevent it should i update my Db model logic or Is there a view level validation that i can implement for same?
My Current Model includes User(Default User Model), Agency and Lcoation Models And Each user can create multiple agencies but each agency can have one or more location but not at the same place.Below is how i have set it up is there any to identify if the string is same?
One way i though was that i Should query and concat all the fields to lower from Location model before saving it and compare it with the new data and only if its different i should update it .Is it a efficient/valid way to do it?
Here is the model i am using Any Advice is appreciated regarding any general mistakes made as well not related to the query.
# Create your models here.
class Agency(models.Model):
agency_sector_choices = [('Transportation','Transportation'),('FireStation','FireStation'),
('Natural&ManmadeDisaster','Natural&ManmadeDisaster'),
('Infrastructure','Infrastructure'),('Health','Health'),
('NGO','NGO'),('Others','Others')]
ngo_sector_choices = [("True","Yes"), ("False","NO")]
agency_admin = models.ForeignKey(User,on_delete=models.PROTECT)
agency_name = models.CharField(max_length=300)
agency_sector = models.CharField(choices=agency_sector_choices)
ngo_sector = models.BooleanField(choices=ngo_sector_choices, null=True, blank=True,default='False')
govt_registered = models.BooleanField(choices=ngo_sector_choices,default="False")
govt_issued_document = models.FileField(null=True, blank=True)
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,16}$', message="Phone number must be entered in the format: '+999999999'. Up to 16 digits allowed.")
phone_number = models.CharField(validators=[phone_regex], max_length=17, blank=True) # Validators should be a list
agency_email = models.EmailField(null=True,blank=True)
def __str__(self):
return f'{self.agency_name}'
class Location(models.Model):
agency_id = models.ForeignKey(Agency,on_delete=models.SET("Deleted User"))
country = models.CharField(max_length=75)
state = models.CharField(max_length=50)
district = models.CharField(max_length=50)
taluka = models.CharField(max_length=50)
city = models.CharField(max_length=50)
address = models.CharField(max_length=250)
latitude = models.CharField(max_length=100,default=21.7679)
longitude = models.CharField(max_length=100,default=78.8718)
def __str__(self):
return f'{self.agency_id}\n{self.country}\n{self.state}\n{self.district}\n{self.taluka}\n{self.city}\n{self.address}\n{self.latitude}\n{self.longitude}'
Solution
To prevent duplication of data you can set unique=True
on database fields. That will ensure you can't have duplication in the data within a column.
You can also use unique_together
on a model meta to define a group of columns which as group can't have the same data between rows.
Answered By - markwalker_
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.