1π
The unique_together
is a constraint, not an id-generator.
To achieve what you want, just an integer field to your SecondModel
, add this field to your unique_together
and override the save
method of the model to set the correct value of this field. For instance, you can do something like this:
class SecondModel(models.Model):
name = models.charField(max_length=30)
mainmodel = models.ForeignKey(MainModel)
mainid = models.Integerfield()
def save(self, *args, **kwargs):
if not self.mainid:
self.mainid = SecondModel.objects.get(mainmodel=self.mainmodel).count()+1
super(SecondModel, self).save(*args, **kwargs)
class Meta:
unique_together(("mainmodel","mainid"),)
0π
Firstly, your question text completely contradicts your title. Using unique_together
in that way implies that you only want one SecondModel for each individual MainModel: in which case a OneToOne field is more appropriate than a ForeignKey with unique_together.
Secondly, what you are asking is impossible. Primary keys are unique: you canβt have multiple instances of SecondModel all with the same id value, even if they have separate parents.
Perhaps you should explain what problem you are actually trying to solve here.
- [Answer]-Django automatically fill user detail
- [Answer]-Newbie need help on a django view element
- [Answer]-Django 1.6.1 and South
- [Answer]-How do I format my text areas in django with bootstrap
0π
The ids are asigned by model β¦ so if you need any other specification, you will have to make them.
- [Answer]-Environment error while installing django-cms on Ubuntu
- [Answer]-Django: Can I do a div ajax refresh in template on object votes and IDs without going to Views?
- [Answer]-How to store json data in static file in Django view, and call this function from another function ?