0

I have two django models. Every model has n number of objects(rows in database). I want to select an object(a row) from model 1 and another object from model 2. I want to use fields of selected objects to create an object in model 3 using actions or otherwise. Please help me.

My admin.py

from django.contrib import admin
from .models import ( FirstMiningMaterial, ProcessFirstStage, 
                     SecondStageMaterials)


def calculate_second_stage_inputs(modeladmin, request, queryset):
    items_list = FirstMiningMaterial.objects.all()
    processing_list = ProcessFirstStage.objects.all()
    for obj in items_list:
        cost1 = obj.iron
        cost2 = obj.rods
        cost3 = obj.petrol
        cost4 = obj.diesel
        cost5 = obj.computers

    for obj in processing_list:
        cost1 += obj.iron_processing_cost
        cost2 += obj.rods_processing_cost
        cost3 += obj.petrol_processing_cost
        cost4 += obj.diesel_proessing_ost
        cost5 += obj.computing_cost

    queryset.update(iron_current_cost=cost1)
    queryset.update(rods_current_cost=cost2)
    queryset.update(petrol_current_cost=cost3)
    queryset.update(diesel_current_cost=cost4)
    queryset.update(computing_current_cost=cost5)

class AdminFirstMiningMaterial(admin.ModelAdmin):
    list_display = ('iron', 'rods', 'petrol', 'diesel', 'computers')
    list_display_links = ['iron', 'rods']
    actions = None
admin.site.register(FirstMiningMaterial, AdminFirstMiningMaterial)


class AdminProcessFirstStage(admin.ModelAdmin):
    list_display = ('iron_processing_cost',
                    'rods_processing_cost',
                    'petrol_processing_cost',
                    'diesel_proessing_ost',
                    'computing_cost')
    actions = None
admin.site.register(ProcessFirstStage, AdminProcessFirstStage)


class  AdminSecondStageMaterils(admin.ModelAdmin):
    list_display = ('iron_current_cost',
                    'rods_current_cost',
                    'petrol_current_cost',
                    'diesel_current_cost',
                    'computing_current_cost')
    actions = [calculate_second_stage_inputs]
admin.site.register(SecondStageMaterials, AdminSecondStageMaterils)

and my models.py from django.db import models

# Create your models here.
class FirstMiningMaterial(models.Model):
    iron      = models.IntegerField()
    rods      = models.IntegerField()
    petrol    = models.IntegerField()
    diesel    = models.IntegerField()
    computers = models.IntegerField()


class ProcessFirstStage(models.Model):
    first_mining           = models.ForeignKey(FirstMiningMaterial, 
  on_delete=models.CASCADE)
    iron_processing_cost   = models.IntegerField()
    rods_processing_cost   = models.IntegerField()
    petrol_processing_cost = models.IntegerField()
    diesel_proessing_ost   = models.IntegerField()
    computing_cost         = models.IntegerField()


class SecondStageMaterials(models.Model):

    second_stage     = models.ForeignKey(ProcessFirstStage, 
     on_delete=models.CASCADE, default=0)
    iron_current_cost = models.IntegerField(default=0)
    rods_current_cost = models.IntegerField(default=0)
    petrol_current_cost = models.IntegerField(default=0)
    diesel_current_cost = models.IntegerField(default=0)
    computing_current_cost = models.IntegerField(default=0)

STATUS_CHOICES = (
    ('d', 'Draft'),
    ('p', 'Published'),
    ('w', 'Withdrawn'),
)

class Article(models.Model):
    title = models.CharField(max_length=100)
    body = models.TextField()
    status = models.CharField(max_length=1, choices=STATUS_CHOICES)

    def __str__(self):
        return self.title
Ashok Kumar
  • 121
  • 8
  • What have you tried so far? What errors are you getting? – Ralf Jun 06 '18 at 11:44
  • I tried to write an action on third model. It is working fine if I have one object in model1 and model2. If I enter one more object in model1 then it is taking first object. – Ashok Kumar Jun 06 '18 at 11:54
  • 1
    Sorry, but I'm having a hard time understanding the explanation. You'll need to add the relevant code parts to your question, so we can help. – Ralf Jun 06 '18 at 11:57

0 Answers0