mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 15:15:42 +00:00 
			
		
		
		
	db unique_together
This commit is contained in:
		@@ -96,7 +96,7 @@ class PartParameterTemplate(models.Model):
 | 
				
			|||||||
    ready to be copied for use with a given Part.
 | 
					    ready to be copied for use with a given Part.
 | 
				
			||||||
    A PartParameterTemplate can be optionally associated with a PartCategory
 | 
					    A PartParameterTemplate can be optionally associated with a PartCategory
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    name = models.CharField(max_length=20)
 | 
					    name = models.CharField(max_length=20, unique=True)
 | 
				
			||||||
    units = models.CharField(max_length=10, blank=True)
 | 
					    units = models.CharField(max_length=10, blank=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Parameter format
 | 
					    # Parameter format
 | 
				
			||||||
@@ -138,32 +138,13 @@ class CategoryParameterLink(models.Model):
 | 
				
			|||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        verbose_name = "Category Parameter"
 | 
					        verbose_name = "Category Parameter"
 | 
				
			||||||
        verbose_name_plural = "Category Parameters"
 | 
					        verbose_name_plural = "Category Parameters"
 | 
				
			||||||
 | 
					        unique_together = ('category', 'template')
 | 
				
			||||||
 | 
					 | 
				
			||||||
class PartParameterManager(models.Manager):
 | 
					 | 
				
			||||||
    """ Manager for handling PartParameter objects
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def create(self, *args, **kwargs):
 | 
					 | 
				
			||||||
        """ Prevent creation of duplicate PartParameter
 | 
					 | 
				
			||||||
        """
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        part_id = kwargs['part']
 | 
					 | 
				
			||||||
        template_id = kwargs['template']
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        params = self.filter(part=part_id, template=template_id)
 | 
					 | 
				
			||||||
        if len(params) > 0:
 | 
					 | 
				
			||||||
            return params[0]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return super(PartParameterManager, self).create(*args, **kwargs)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PartParameter(models.Model):
 | 
					class PartParameter(models.Model):
 | 
				
			||||||
    """ PartParameter is associated with a single part
 | 
					    """ PartParameter is associated with a single part
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    objects = PartParameterManager()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    part = models.ForeignKey(Part, on_delete=models.CASCADE, related_name='parameters')
 | 
					    part = models.ForeignKey(Part, on_delete=models.CASCADE, related_name='parameters')
 | 
				
			||||||
    template = models.ForeignKey(PartParameterTemplate)
 | 
					    template = models.ForeignKey(PartParameterTemplate)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -31,44 +31,26 @@ class Project(models.Model):
 | 
				
			|||||||
    description = models.CharField(max_length=500, blank=True)
 | 
					    description = models.CharField(max_length=500, blank=True)
 | 
				
			||||||
    category = models.ForeignKey(ProjectCategory, on_delete=models.CASCADE, related_name='projects')
 | 
					    category = models.ForeignKey(ProjectCategory, on_delete=models.CASCADE, related_name='projects')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    class Meta:
 | 
				
			||||||
 | 
					        unique_together = ('name', 'category')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __str__(self):
 | 
					    def __str__(self):
 | 
				
			||||||
        return self.name
 | 
					        return self.name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ProjectPartManager(models.Manager):
 | 
					 | 
				
			||||||
    """ Manager for handling ProjectParts
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def create(self, *args, **kwargs):
 | 
					 | 
				
			||||||
        """ Test for validity of new ProjectPart before actually creating it.
 | 
					 | 
				
			||||||
        If a ProjectPart already exists that references the same:
 | 
					 | 
				
			||||||
        a) Part
 | 
					 | 
				
			||||||
        b) Project
 | 
					 | 
				
			||||||
        then return THAT project instead.
 | 
					 | 
				
			||||||
        """
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        project_id = kwargs['project']
 | 
					 | 
				
			||||||
        part_id = kwargs['part']
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        project_parts = self.filter(project=project_id, part=part_id)
 | 
					 | 
				
			||||||
        if len(project_parts) > 0:
 | 
					 | 
				
			||||||
            return project_parts[0]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return super(ProjectPartManager, self).create(*args, **kwargs)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class ProjectPart(models.Model):
 | 
					class ProjectPart(models.Model):
 | 
				
			||||||
    """ A project part associates a single part with a project
 | 
					    """ A project part associates a single part with a project
 | 
				
			||||||
    The quantity of parts required for a single-run of that project is stored.
 | 
					    The quantity of parts required for a single-run of that project is stored.
 | 
				
			||||||
    The overage is the number of extra parts that are generally used for a single run.
 | 
					    The overage is the number of extra parts that are generally used for a single run.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    objects = ProjectPartManager()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    part = models.ForeignKey(Part, on_delete=models.CASCADE)
 | 
					    part = models.ForeignKey(Part, on_delete=models.CASCADE)
 | 
				
			||||||
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
 | 
					    project = models.ForeignKey(Project, on_delete=models.CASCADE)
 | 
				
			||||||
    quantity = models.PositiveIntegerField(default=1)
 | 
					    quantity = models.PositiveIntegerField(default=1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    class Meta:
 | 
				
			||||||
 | 
					        unique_together = ('part', 'project')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    # TODO - Add overage model fields
 | 
					    # TODO - Add overage model fields
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,9 +17,7 @@ class StockLocation(InvenTreeTree):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class StockItem(models.Model):
 | 
					class StockItem(models.Model):
 | 
				
			||||||
    part = models.ForeignKey(Part,
 | 
					    part = models.ForeignKey(Part, on_delete=models.CASCADE, related_name='locations')
 | 
				
			||||||
                             on_delete=models.CASCADE,
 | 
					 | 
				
			||||||
                             related_name='locations')
 | 
					 | 
				
			||||||
    location = models.ForeignKey(StockLocation, on_delete=models.CASCADE)
 | 
					    location = models.ForeignKey(StockLocation, on_delete=models.CASCADE)
 | 
				
			||||||
    quantity = models.PositiveIntegerField()
 | 
					    quantity = models.PositiveIntegerField()
 | 
				
			||||||
    updated = models.DateField(auto_now=True)
 | 
					    updated = models.DateField(auto_now=True)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -78,6 +78,9 @@ class SupplierPriceBreak(models.Model):
 | 
				
			|||||||
    quantity = models.PositiveIntegerField()
 | 
					    quantity = models.PositiveIntegerField()
 | 
				
			||||||
    cost = models.DecimalField(max_digits=10, decimal_places=3)
 | 
					    cost = models.DecimalField(max_digits=10, decimal_places=3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    class Meta:
 | 
				
			||||||
 | 
					        unique_together = ("part", "quantity")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __str__(self):
 | 
					    def __str__(self):
 | 
				
			||||||
        return "{mpn} - {cost}{currency} @ {quan}".format(
 | 
					        return "{mpn} - {cost}{currency} @ {quan}".format(
 | 
				
			||||||
            mpn=self.part.MPN,
 | 
					            mpn=self.part.MPN,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user