Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing list of objects in Django model

Tags:

python

django

Is there a way in Django to have multiple objects stored and manageable (in Django admin) inside another object?

Example, I have two models: Items and RMA. The RMA may have multiple Items inside of it. Each Item is unique in the sense that it is an inventoried part, so I can't just reference the same item multiple times with foreignKey (though maybe I'm misunderstanding its use/implementation).

So for now, I have an Item model:

class Item(models.Model):
    serial_number = models.CharField(max_length=200)
    name = models.CharField(max_length=200)
    part_number = models.CharField(max_length=200)
    location = models.CharField(max_length=200)
    def __unicode__(self):
        return self.name

And an RMA model:

class RMA(models.Model):
    number = models.AutoField(primary_key=True)
    items = ?????

Ultimately I'd like to be able to maintain use of the Django admin functionality to add/remove items from an RMA if necessary, so I've been staying away from serializing a list and then deserializing on display. Any help would be much appreciated.

like image 586
szumlins Avatar asked May 27 '15 01:05

szumlins


People also ask

How to save data in models in Django?

Creating objectsTo create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. This performs an INSERT SQL statement behind the scenes. Django doesn't hit the database until you explicitly call save() . The save() method has no return value.

What are QuerySets in Django?

A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.

What does objects all () do in Django?

all() Returns a copy of the current QuerySet (or QuerySet subclass). This can be useful in situations where you might want to pass in either a model manager or a QuerySet and do further filtering on the result. After calling all() on either object, you'll definitely have a QuerySet to work with.

How do I store a list in Django?

A simple way to store a list in Django is to just convert it into a JSON string, and then save that as Text in the model. You can then retrieve the list by converting the (JSON) string back into a python list. Here's how: class MyModel (models.Model): myList = models.TextField (null=True) # JSON-serialized (text) version of your list

What are model data types and fields in Django?

Django model data types and fields list. The most important part of a model and the only required part of a model is the list of database fields it defines. Fields are specified by class attributes. Be careful not to choose field names that conflict with the models API like clean, save, or delete.

What is an instance of a field class in Django?

Each field in the model should be an instance of the appropriate Field class. Django uses field class types to determine a few things: The column type, which tells the database what kind of data to store (e.g. INTEGER, VARCHAR, TEXT).

Can I use commoninfo model as a base class in Django?

Instead, when it is used as a base class for other models, its fields will be added to those of the child class. The Student model will have three fields: name, age and home_group. The CommonInfo model cannot be used as a normal Django model, since it is an abstract base class.


1 Answers

You're modeling a has-many relationship.

This would be modeled with a Foreign Key on Item to RMA:

class Item(models.Model):
    serial_number = models.CharField(max_length=200)
    name = models.CharField(max_length=200)
    part_number = models.CharField(max_length=200)
    location = models.CharField(max_length=200)
    rma = models.ForeignKey(RMA)
    def __unicode__(self):
        return self.name

To make it accessible in the admin of RMA you need djangos InlineAdmin functionality.

You can find examples in the django tutorial part2.

like image 88
Sebastian Wozny Avatar answered Sep 22 '22 13:09

Sebastian Wozny