The title may be a little confusing here, so let me explain.
Firstly I have a model of a list of items which is a foreign key of another model. The foreign key object has access wh_item_id and wh_item_name. I am trying to put that information into this format
wh_item_id=wh_item_name
So for example it will return as:
102944=Hands of the Light
Now the part where it gets tricky is that I wish each field in the model to be put into this string, and then into a list that can be accessed later. Minus any blank fields in the model.
The original model:
class ProtectionList(models.Model):
character = models.ForeignKey(Character)
main_hand = models.ForeignKey(Loot, related_name="Main Hand", blank=True, null=True)
off_hand = models.ForeignKey(Loot, related_name="Off Hand", blank=True, null=True)
head = models.ForeignKey(Loot, related_name="Head", blank=True, null=True)
neck = models.ForeignKey(Loot, related_name="Neck", blank=True, null=True)
shoulder = models.ForeignKey(Loot, related_name="Shoulder", blank=True, null=True)
back = models.ForeignKey(Loot, related_name="Back", blank=True, null=True)
chest = models.ForeignKey(Loot, related_name="Chest", blank=True, null=True)
wrist = models.ForeignKey(Loot, related_name="Wrist", blank=True, null=True)
hands = models.ForeignKey(Loot, related_name="Hands", blank=True, null=True)
waist = models.ForeignKey(Loot, related_name="Waist", blank=True, null=True)
legs = models.ForeignKey(Loot, related_name="Legs", blank=True, null=True)
feet = models.ForeignKey(Loot, related_name="Feet", blank=True, null=True)
ring1 = models.ForeignKey(Loot, related_name="Ring 1", blank=True, null=True)
ring2 = models.ForeignKey(Loot, related_name="Ring 2", blank=True, null=True)
trinket1 = models.ForeignKey(Loot, related_name="Trinket 1", blank=True, null=True)
trinket2 = models.ForeignKey(Loot, related_name="Trinket 2", blank=True, null=True)
So there could be anything up to 16 items in this list, however I need to remove anything in list that shows as None.
For Example,
main_hand returns an object off_hand returns None head returns an object ... (I'll just use 3 fields for now for simplicity)
I wish the list to look like the following:
item_list = [1234=main hand itemname,5678=head itemname]
Missing out the off_hand.
Loot model for reference
class Loot(models.Model):
wh_item_id = models.CharField(verbose_name="Wowhead Item ID", max_length=255)
wh_item_name = models.CharField(verbose_name="Wowhead Item Name", max_length=255)
gear_type = models.CharField(max_length=255, blank=True, null=True)
lockout_tier = models.IntegerField(blank=True, null=True)
EDIT:
What I essentially am after is the following:
item_list = [item_list.main_hand.wh_item_id + '=' + item_list.main_hand.wh_item_name,item_list.off_hand.wh_item_id + '=' + item_list.off_hand.wh_item_name,item_list.head.wh_item_id + '=' + item_list.head.wh_item_name,item_list.neck.wh_item_id + '=' + item_list.neck.wh_item_name,item_list.shoulder.wh_item_id + '=' + item_list.shoulder.wh_item_name,item_list.back.wh_item_id + '=' + item_list.back.wh_item_name,item_list.main_hand.wh_item_id + '=' + item_list.chest.wh_item_name,item_list.main_hand.wh_item_id + '=' + item_list.main_hand.wh_item_name,item_list.main_hand.wh_item_id + '=' + item_list.main_hand.wh_item_name,item_list.main_hand.wh_item_id + '=' + item_list.main_hand.wh_item_name,item_list.main_hand.wh_item_id + '=' + item_list.main_hand.wh_item_name,item_list.main_hand.wh_item_id + '=' + item_list.main_hand.wh_item_name,item_list.main_hand.wh_item_id + '=' + item_list.main_hand.wh_item_name,item_list.main_hand.wh_item_id + '=' + item_list.main_hand.wh_item_name,item_list.main_hand.wh_item_id + '=' + item_list.main_hand.wh_item_name,item_list.main_hand.wh_item_id + '=' + item_list.main_hand.wh_item_name]
But I only want the items that do not return None to be in that list.