Move back two links in class structure

Here’s the general data structure I’m after:
this_class has a link to previous_class which has a link to class_before_that.

I want to fetch a date, a select and an int from class_before_that. What is the best way to go about this? There could be multiple instances of each class.

  • I know this could be done with an SQL query but this seems inefficient and un-pythonic (inasmuch as I understand it).

  • Is the .get() method the right way to go here? Link to a similar resolved issue though this refers to child rather than peer/ parent/ grandparent.

  • There must be a way to call something like this_class.previous_class.class_before_that(‘date’). This seems like the best way to go, it’s just following the pointer.

Classes are not my strong suit though ERPNext’s structure has been a great challenge and learning experience for me.
-T

@tmatteson will be great if you can explain a specific use case.

Sure! Again with the scalable agricultural businesses, I’m working on application that models field crops:

  1. ‘Variety’ is a doctype that stores constants about a specific variety of a field crop, like “Red Fire Lettuce”. This hold things like the life cycle (annual/perennial) and day to maturity. This means you enter the seed and growth information once.

  2. ‘Planting’ doctype holds an ‘instantiation’ of that crop, with things like “planting date” and “location” and links to ‘variety’.

  3. ‘Harvest Crop’ is the doctype that captures the yield of the ‘planting’ and then submits that yield to the stock module.

I want to test that the planting date is before the harvest date, and also to warn if the crop is before harvested before its days to maturity. That means following the links backwards to the previous doctype.

@rmehta, clear as mud, right?

I think I’d like to simplify this question: when I try to return a link from one doctype to another, what datatype should I expect? What can I call on on that? I assume there are Frappe API calls for this but I’m not sure where to look.

I’ll put this here for reference: I was able to solve this by using frappe.db.get_value and modifying another doctype to have a read only field that contained the link I needed.

The key was to use the link field to point to the specific doctype, eg:
r = frappe.db.get_value(“previous_class”, self.link_to_class_before_that, “value_i_wanted”)

Or in my specific code (which I don’t think is a clearer example, but here it is):
planting_days_to_maturity = frappe.db.get_value(“Planting”, self.crop_harvested, “variety_days_to_maturity”)

I don’t consider this a particularly elegant solution but it’s functional and it returns what I expected. I have found lots of useful code snippets here on the forum so I wanted to share this one back. I spent a lot of time working this out, so if anybody has a similar issue, I’ll do my best to help.