Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django many-to-many relationship with extra fields

I am building a simple interface to a biological database using the django-admin to populate the db. I want tot to use a many-to-many relationship for a questionnaire to fish species (one questionnaire can have more than one species and one species can be present in more than one questionnaire). The two models in question:

class Species(models.Model):
    fish_spp_name = models.CharField(max_length=255, unique=True)


class Questionaire(models.Model):
    # ...
    fish_caught = models.ManyToManyField(Species)

now, I want to my data to contain a number of each species caught, per questionnaire. So, for example, I can associate 3 different species with questionnaire id=1, but how do I include that, say 2 of the first species, 1 of the second and 4 of the third were caught?

like image 588
Darwin Tech Avatar asked Aug 15 '11 00:08

Darwin Tech


People also ask

How does Django handle many-to-many relationship?

Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship. By default, this table name is generated using the name of the many-to-many field and the name of the table for the model that contains it.

How do I get rid of many-to-many field data in Django?

ManyToMany field has some methods that you can call: add(Object) remove(Object) clear() this one is for removing all objects.

How do I create a one to many relationship in Django?

To define a one to many relationship in Django models you use the ForeignKey data type on the model that has the many records (e.g. on the Item model).

What is through in Django models?

The through attribute/field is the way you customize the intermediary table, the one that Django creates itself, that one is what the through field is changing.


1 Answers

Check this: Extra fields on many-to-many relationships

like image 78
ypercubeᵀᴹ Avatar answered Oct 05 '22 00:10

ypercubeᵀᴹ