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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:50:05+00:00 2026-05-24T09:50:05+00:00

UPDATED ANSWER TO QUESTION: How do I get a lookup value (from Atc) to

  • 0

UPDATED ANSWER TO QUESTION: How do I get a lookup value (from Atc) to display on a transaction table (Drugs) when displaying Drug?

Solution, using Saverio’s approach of embedding the relationship in the model itself:

models.py

class Atc(models.Model):
    id = models.CharField(max_length=60, primary_key=True, db_column='ID') 
    txt = models.CharField(max_length=690, db_column='TXT')
    class Meta:
        db_table = u'ATC'
    def __unicode__(self):
    return u"%s - %s" % (self.id, self.txt)

class Drug(models.Model):
    id = models.CharField(max_length=64, primary_key=True, db_column='ID')
    atc = models.ForeignKey(Atc, db_column='ATCCD')
    pricetopharm = models.FloatField(db_column='PRICETOPHARM')
    brandnm = models.CharField(max_length=135, db_column='BRANDNM')
    drugnm = models.CharField(max_length=240, db_column='DRUGNM')
    class Meta:
        db_table = u'DRUGS'
    def __unicode__(self):
    return u"%s - %s" %(self.drugnm, self.formandstrength)

And in admin.py

class DrugsAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['id']}),
        ('ATC',          {'fields': ['atc','brandnm'] }),
        (None,           {'fields': ['drugnm']}),
        ('Prices',       {'fields': ['pricetopharm']}),
    ]
    search_fields = ['drugnm','brandnm']

admin.site.register(Drug, DrugsAdmin)
admin.site.register(Atc)

The key points are to add to models.py

atc = models.ForeignKey(Atc, db_column='ATCCD')

to the Drug (transaction, not reference table) and

('ATC',          {'fields': ['atc','brandnm'] }),

to the admin.py fieldsets to represent the many-to-one relationship.

Now, the contents of the admin.py is less relevant (which it should be in the MTV philosophy. Also, renaming the objects in the singular (Drug, not Drugs) was helpful, if nothing else other than it go rid of the duplicate s’s (ie. Drugs not Druggs).
The result is a display of drugs shows a dropdown box of id + txt eg. P04H11 – Cytotoxics

Thanks for your help guys
Pete

(too junior burger to answer my own questions, hence edit of original question!)

  • 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-24T09:50:06+00:00Added an answer on May 24, 2026 at 9:50 am

    Why not just

    class Atc(models.Model): # in models.py
        drug = models.ForeignKey('Drug')
        description = models.CharField(max_length=10000)
        def __unicode__(self): return u"%s - %s" % (self.id, self.description)
    
    class AtcInline(admin.StackedInline): # in admin.py
        # set option for not being able to add more than one.
        model = Atc
        fields = ['description']
    

    This will display the related atc for your Drug instance right in the inlines part of the form.

    (also, it could be interesting to note that generally django models are named in singular, as in Drug instead of Drugs. This is semantically correct with Drug.objects.filter(), drug = Drug() etc)

    Edit: further considerations on DB schema and data model

    It seems that you have a previous DB schema to use. If you can’t change column names, you can at least give your attributes more meaningful names, so code is much more readable:

    class AtcClassification(models.Model):
        code = models.CharField(max_length=60, primary_key=True, db_column='ID') 
        description = models.CharField(max_length=690, db_column='TXT')
    
    class Drug(models.Model):
        id = models.CharField(max_length=64, primary_key=True, db_column='ID')
        atc_classification = models.ForeignKey(AtcClassification, db_column='ATCCD')
        price_to_pharmacy = models.FloatField(db_column='PRICETOPHARM')
        brand_name = models.CharField(max_length=135, db_column='BRANDNM')
        drug_name = models.CharField(max_length=240, db_column='DRUGNM')
        form_and_strength = models...
    
        class Meta:
            db_table = u'DRUGS'
    
        def __unicode__(self):
            return u"%s - %s" %(self.drug_name, self.form_and_strength)
    
        def atc_description(self): return self.atc_classification.description
        def atc_code(self): return self.atc_classification.code
    

    If you own also the DB schema, once the data model is complete, use something like South migrations (db.rename(table_name, old_column_name, new_column_name)) to synchronize the schema to your now readable data model.

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

Sidebar

Related Questions

Updated 10/21: Changed title and question in order to possibly get an answer (other
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class
I know that I can't get a specific answer to my question, but I
I'm looking for an updated answer to this question . It seems that Event.observers
UPDATED QUESTION WITH ANSWER!! Original Question I've been looking at Eli's object.watch (https://gist.github.com/384583) script
NOTE: I finally found an exact duplicate question and good answer at: Get script
Based on this answer Get the subdomain from a URL , I am trying
Update2 Please see my answer to my question. Update So, I updated the code
See my previous question . I updated the code as per Kevin P's answer
I am new here and I hope I will get answer to my question.

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.