Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7588113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:50:35+00:00 2026-05-30T19:50:35+00:00

I’m trying to figure out how to design my model. I’ve been going over

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:50:36+00:00Added an answer on May 30, 2026 at 7:50 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.