-2

I have a model.py that link 3 Classes to a USER

class Organization_Information(models.Model):

    Organization_name = models.CharField(max_length=25)
    Organization_address = models.CharField(max_length=40)
    Organization_admin = models.OneToOneField(MyUser, on_delete=models.CASCADE, null=True, blank=True)

class Project(models.Model):

    project_name = models.CharField(max_length= 25, default='')
    organisation_name = models.ForeignKey('Organization_Information', on_delete=models.CASCADE)

class Asset(models.Model):

    asset_note = models.TextField(default='',)
    project_name = models.ForeignKey('Project', on_delete=models.CASCADE)

Each Instance of a Class refer to the above, And the First Refer to the USER"MyUser" custom model.

I got Stuck on the View I tried re-reading it and figuring it out But I got Stuck.

Almost All the "walk-through" tutorial and explanation are old and invalid.

Using "Django==2.0.7 - Python3.6"

All I want is to display the model Fields into the Views.py and their child

for e.g: MyUser ID = 3, and The Organization_Information refer to it By the ID, I want to display all the Information from The User Information to the Assets Information to be viewed depending on the user ID when He/She Login.

I tried to user Query-set But it didn't work as desired is their any simple solution.

Thank you.

Alex
  • 7
  • 6

2 Answers2

0

try like this , still there are so many ways to do it , check official doc

    x=MyUser.objects.get(id=1)
    x.organization_information.Organization_name
    u'llc'

    >>> x.organization_information.Organization_address
    u'new york'
    >>> x.organization_information.Organization_admin
    <User: hi>
    >>> 

    y=x.organization_information.project_set.first()
    >>> y.project_name
    u'app'
    >>> y.organisation_name
    <Organization_Information: Organization_Information object>
    z=y.asset_set.first()
    >>> z.asset_note
    u'train'
    >>> z.project_name
    <Project: Project object>
    >>> z.project_name.project_name
    u'app'
giveJob
  • 1,500
  • 3
  • 17
  • 29
  • It's working perfectly, but it's not so efficient and not stable. – Alex Jul 17 '18 at 13:12
  • I read it, again and again trying to figure it out. but as I stated Above I couldn't comprehend it well. Thank you much for the snippet above. and Have a good day – Alex Jul 17 '18 at 13:19
0

Use dict() or .(double underscore)dict(double underscore) you will be able to reach all metadata from your classes and heritage, but this brings all other functions too, you will have to add some logic to get the data that you want

Or you can just run [f.name for f in MyModel._meta.get_fields()]

Read more in this other question: Django: Get list of model fields?

Diego Vinícius
  • 2,125
  • 1
  • 12
  • 23