Zee
Zee

Reputation: 155

Figuring out how to design my model and using "through"

I'm trying to figure out how to design my model. I've been going over the documentation, and it ultimately seems like I should be using the "through" attribute, but I just can't figure out how to get it to work how I want.

If someone could take a look and point out what I'm missing, that would be really helpful. I have pasted my model below.

This is what I am trying to do:

1) Have a list of server types

2) Each server type will need to have different parts available to that specific server type

3) The asset has a FK to the servermodel, which has a M2M to the parts specific to that server type.

My question is, how can each "Asset" store meta data for each "Part" specific to that "Asset"? For example, each "Asset" should have it's own last_used data for the part that's assigned to it.

Thanks! :)

class Part(models.Model):
    part_description = models.CharField(max_length=30,unique=1)
    last_used = models.CharField(max_length=30)
    def __unicode__(self):
        return self.part_description

class ServerModel(models.Model):
    server_model = models.CharField(max_length=30,unique=1)
    parts = models.ManyToManyField(Part)
    def __unicode__(self):
        return self.server_model

class Asset(models.Model):
    server_model = models.ForeignKey(ServerModel)
    serial_number = models.CharField(max_length=10,unique=1)
    def __unicode__(self):
        return self.server_model.server_model

EDIT: Thank you for the help!

I may have not explained myself clearly, though. It's probably my confusing model names.

Example: ServerModel stores the type of server being used, say "Dell Server 2000".

The "Dell Server 2000" should be assigned specific parts: "RAM" "HARD DISK" "CDROM"

Then, I should be able to create 10x Assets with a FK to the ServerModel. Now, each of these assets should be able to mark when the "RAM" part was last used for this specific asset.

Upvotes: 2

Views: 66

Answers (1)

marue
marue

Reputation: 5726

I'm not sure I exactly understand what you want to do, but basically you can solve that with a "through" model, as you expected:

import datetime

class Part(models.Model):
    name = models.CharField(max_length=30,unique=1)

class ServerModel(models.Model):
    server_model = models.CharField(max_length=30,unique=1)
    parts = models.ManyToManyField(Part,through='Asset')

class Asset(models.Model):
    server_model = models.ForeignKey(ServerModel)
    part = models.ForeignKey(Part)
    serial_number = models.CharField(max_length=10,unique=1)
    used = models.DateTimeField(default=datetime.datetime.now())

First thing to notice is the relation of the parts to the servermodel using the "through"-model: that way for each Part instance assigned to the "parts"-property of a ServerModel instance a new Asset instance is created (Phew - hope that doesn't sound too complicated). At the time of creation the "used"-property of the Asset instance is set to the current date and time (thats what default=datetime.datetime.now() does).

If you do that, you can then just query the database for the last asset containing your part. That queryset can then be sorted by the "used" property of the Asset model, which is the date when the Asset instance has been created.

ServerModel.objects.filter(parts__name='ThePartYouAreLookingFor').order_by('asset__used')

I'm not absolutely sure if the queryset is correct, so if someone finds huge nonsense in it, feel free to edit ;)

edit: The models above do not exactly that. But you do not even need a through model for what you want:

class ServerModel(models.Model):
    server_model = models.CharField(max_length=30,unique=1)
    parts = models.ManyToManyField(Part)

class Asset(models.Model):
    server_model = models.ForeignKey(ServerModel)
    parts = models.ForeignKey(Part)
    serial_number = models.CharField(max_length=10,unique=1)
    used = models.DateTimeField(default=datetime.datetime.now())

Basically you can just add assets and then query all assets that have a RAM in parts.

Asset.objects.filter(parts__contains='RAM').order_by('used')

Get the date of the first (or last) result of that queryset and you have the date of the last usage of your 'RAM'-part.

Upvotes: 1

Related Questions