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 6939533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:38:05+00:00 2026-05-27T12:38:05+00:00

Hopefully this isn’t too ugly, I just started moving over from PHP to Django,

  • 0

Hopefully this isn’t too ugly, I just started moving over from PHP to Django, but I’m trying to wrap my head around models and can’t quite figure out how to approach this.

Here is what I’m trying to do:

1) Create a new “configuration” (set RamConfig.size to say 16GB)

2) Select which sizes and quantities from SIZE_CHOICES to include in the 16GB config; For example, lets say I added eight 2GB “parts” to the 16GB “config”.

3) Save config.

The above works just fine, but now we have a RamConfig that is 16GB with (8) 2GB “parts”. I want each of these 2GB parts to reference assets from the “Asset” model where RamConfig.servermodel is in Asset.Servermodels.

So, if the 16GB “config” is pulled up, it will display all available assets for each 2GB “part”. I’m really not sure if this should be done in the model or the template; I’m just trying to get it to function properly in admin for now.

from django.db import models

class Manufacturer(models.Model):
    name = models.CharField(max_length=20,unique=1)
    def __unicode__(self):
        return self.name

class ServerModel(models.Model):
    name = models.CharField(max_length=20,unique=1)
    manufacturer = models.ForeignKey(Manufacturer)
    def __unicode__(self):
        return "%s %s" % (self.manufacturer, self.name)

class Asset(models.Model):
    SIZE_CHOICES = (
        ('512MB', '512MB'),
        ('1GB', '1GB'),
        ('2GB', '2GB'),
        ('4GB', '4GB'),
        ('8GB', '8GB'),
        ('16GB', '16GB'),)
    name = models.CharField(max_length=20)
    sku = models.CharField(max_length=10,unique=1)
    oneoff = models.BooleanField()
    size = models.CharField(max_length=8, choices=SIZE_CHOICES)
    servermodels = models.ManyToManyField(ServerModel)
    #Returns a string containing the platforms this asset is linked to.
    def used_on(self):
        usedon = []
        for servermodel in self.servermodels.all():
            usedon.append(servermodel.__str__())

        return ", ".join(usedon)
    def __unicode__(self):
        return self.size


class RamConfig(models.Model):
    size = models.CharField(max_length=5, verbose_name="Configuration Size")
    description = models.CharField(max_length=30)
    servermodel = models.ForeignKey(ServerModel, verbose_name="Server Model")
    assets = models.ManyToManyField(Asset, through='RamConfigPart')

    def __unicode__(self):
        return self.size

    def total_parts(self):
        return self.assets.all().count()



class RamConfigPart(models.Model):
    SIZE_CHOICES = (
        ('512MB', '512MB'),
        ('1GB', '1GB'),
        ('2GB', '2GB'),
        ('4GB', '4GB'),
        ('8GB', '8GB'),
        ('16GB', '16GB'),)
    config_size = models.CharField(max_length=8, choices=SIZE_CHOICES)
    ramconfig = models.ForeignKey(RamConfig)
    asset = models.ForeignKey(Asset)
    quantity = models.PositiveIntegerField()
    def __unicode__(self):
        return self.asset.name

    class Meta:
        unique_together = ['ramconfig', 'asset']

Thanks for any tips :]

  • 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-27T12:38:06+00:00Added an answer on May 27, 2026 at 12:38 pm

    I would re-think that model structure if I were you. And if your problem is to represent how much RAM and how many slots that RAM takes up on the server, I would use two fields to solve that.

    Then you could in your unicode method do something like:

    def __unicode__(self):
        return "%s GB (%s)" % (self.ram, self.slots)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully this isn't too specific/small a question, but I was wondering if anyone knew
Hopefully this isn't too obscure for SO, but consider the following P/Invoke signature: [DllImport(odbc32.dll,
Hopefully this isn't too stupid but I want to make sure I'm doing this
This isn't a very simple question, but hopefully someone has run across it. I
Hopefully this isn't a dupe of another question, but I couldn't see it anywhere
I'm fairly new to Joomla - so hopefully this isn't too simple. My client
Hopefully this isn't too silly of a question. In MVC there appears to be
I have just started looking at javascript so hopefully this will be something simple.
This isn't strictly a programming question, but hopefully someone here has the knowledge to
I'm still early on in learning XML schema, so hopefully this isn't too easy

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.