Django - Primary Key field related to foreign key -


i want instances of feature model share foreign key have id. when new feature created, want auto-increment id.

enter image description here

if take @ print feature table in database, each feature has foreign key (shapefile_id) , share same shapefile_id, have unique id (id_relat)

i know if there better way handle have found far:

class shapefile(models.model):     filename = models.charfield(max_length=255)   class feature(models.model):     shapefile = models.foreignkey(shapefile)     id_relat = models.positiveintegerfield(db_index=true)      def meta(self):         unique_together = (("shapefile", "id_relat"),)      def save(self, *args, **kwargs):         if not self.id_relat: #assign incremented id new feature             self.id_relat = cal_id_relat(self.shapefile)         super(feature, self).save(*args, **kwargs)  def cal_id_relat(shapefile):     ids_relat = feature.objects.filter(shapefile=shapefile).order_by('-id_relat').values_list('id_relat',flat=true)     if ids_relat:         return ids_relat[0]+1     else:         return 0 

what dont in this, it's necessity query features highest id_relat in cal_id_relat method.

your django application executed in >1 threads, approach potentially dangerous. cal_id_relat called simultaneously , return same id calls.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -