user1330974
user1330974

Reputation: 2616

How do I save a list of database models/entities as a field in a model (Django)?

This might be impossible, but I wonder if there's a way to save a list of model as part of another model. For example, suppose I have a model called Group defined as below:

class Group(models.Model):
    name = models.CharField("Group Name", max_length=100)
    created_at = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)

Then I have another model named Message, that is defined as below:

class Message(models.Model):
    content = models.CharField(db_index=True, max_length=160)
    composer = models.ForeignKey(User, related_name='composed_message')
    created_at = models.DateTimeField(auto_now_add=True)
    # groups = a_list_of_group # How do I accomplish this? 

I would like to add a field/column in the model Message above called groups that will point to multiple group entities. Is there a way to do this? What sort of DB relationship must be established between Group and Message to accomplish? Thank you.

Upvotes: 0

Views: 55

Answers (3)

Rahul Gupta
Rahul Gupta

Reputation: 47846

You can use ManyToManyField.

A ManyToManyField in the Message model will mean that a Message can be posted in multiple Groups and a Group can have multiple Message objects.

class Message(models.Model):
    content = models.CharField(db_index=True, max_length=160)
    composer = models.ForeignKey(User, related_name='composed_message')
    created_at = models.DateTimeField(auto_now_add=True)
    groups = models.ManyToManyField(Group) # add a 'ManyToManyField' for storing multiple groups

Here, a model Message object now points to multiple Group entities.

To get all the groups for a particular message object message_obj, you can use below:

message_obj.groups.all() # lists all the groups for a particular message 

To save a group to message_object, you can do the following:

We first create a group object group_obj (lets say). Then to save it we can use .add().

message_object.groups.add(group_obj) # attach the group object to a message

Upvotes: 1

Vladir Parrado Cruz
Vladir Parrado Cruz

Reputation: 2359

@Mark Galloway gave you a good answer, but as he properly commented it is for the case when there is a one to many relationship between Message and Group. You could also have a many to many relation between Message and Group, and then you could use this:

class Message(models.Model):
    content = models.CharField(db_index=True, max_length=160)
    composer = models.ForeignKey(User, related_name='composed_message')
    created_at = models.DateTimeField(auto_now_add=True)
    groups = models.ManyToManyField(Group)

Now you have more options ;).

Upvotes: 1

Mark Galloway
Mark Galloway

Reputation: 4151

You need a ForeignKey from Group to Message

class Group(models.Model):
    name = models.CharField("Group Name", max_length=100)
    created_at = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)
    message = models.Foreignkey(Groups, null=True, related_name="groups")

You can access these groups from a message instance as follows:

message.groups.all()

Note that this means one Group has only have one Message, and that one Message can have any number of groups. Is this what you desire?

Upvotes: 1

Related Questions