Dynamically retrieve data from table

23 views
Skip to first unread message

bobo michel

unread,
Feb 5, 2020, 7:30:58 AM2/5/20
to Django users
Hi all,

I am struggling with a piece of code for a few days now and thought I'll seek for advice.

Here is the storyline:

I have a user, who enter invoices on the database based on the Invoices model. Each invoice is linked to an Entity. The user does not have any link to either the Invoice or the Entity.

When the Entity user owner access their side of the website, I would like to display Invoices which belong to the Entity.

Now my problem is that I can pass a entity when the invoice inputting form is submitted to the database but I cannot filter on the entity specifically.

I want to be able to have a following:

If Entity Y is selected, invoice 123 to be stored as linked to Entity Y
If Entity X is selected, invoice 456 to be stored as linked to Entity X.

Below is the code which works to save the Invoice input into the Invoice model but only with the first Entity in the Entity model. I would like to make it dynamic and not to hard code the Entity ID in the program.

Views.py:
def claim_details(request):                                         #save the invoice onto DB
    form = forms.SaveInvoice(request.POST)
    user = request.user.id
    if request.method == 'POST':
        if form.is_valid():
            instance = form.save(commit=False)
            inv = Entity.objects.all()
            print(inv)
            instance.entity_name = inv
            print(inv)
            instance.save()
            forms.SaveInvoice()

        return redirect('accounts:claim')
    else:
        form = forms.SaveInvoice()
    args = {'form': form}
    return render(request, 'dashboard/claim_details.html', args)

Models.py

class Entity(models.Model):
contact = models.ForeignKey(User, default=None, on_delete=models.CASCADE)
company_name = models.CharField(max_length=40, blank=False, null=True)
vat_registration = models.CharField(max_length=12, blank=False, null=True)
street_number = models.CharField(max_length=10, blank=False, null=True)
street_name = models.CharField(max_length=100, blank=False, null=True)
post_code = models.CharField(max_length=10, blank=False, null=True)
city = models.CharField(max_length=40, blank=False, null=True)
country = models.CharField(max_length=60, blank=False, null=True)
email = models.EmailField(max_length=240, blank=False, null=True)

class Meta:
verbose_name_plural = "Entities"

def __str__(self):
return self.company_name

class Invoices(models.Model):
invoice_number = models.CharField(max_length=12, blank=False, null=True)
invoice_date = models.DateField()
invoice_code = models.CharField(max_length=10, blank=False, null=True)
client_ref = models.CharField(max_length=10, null=True)
supplier = models.ForeignKey(Suppliers, default=None, on_delete=models.CASCADE)
net_amount = models.FloatField()
vat_paid = models.FloatField()
vat_reclaimed = models.FloatField()
invoice_type = models.CharField(max_length=10, blank=False, null=True) # should really be something with choices but for now it will do.
entity_name = models.ForeignKey(Entity, blank=True, null=True, default=None, on_delete=models.CASCADE)

class Meta:
verbose_name_plural = "Invoices"

def __str__(self):
return self.invoice_type


I would like the entity_name ID field to be parsed depending on which entity is logged in the system.

here is the another view I use to display the details of a current Entity in my dashboard - Entity is linked to the user who created the entity.

@login_required()
def client_details(request):
    user = request.user
    company = Entity.objects.filter(contact_id=user.id)
    print(company)
    print(user.id)
    context = {'companies': company}
    return render(request, 'dashboard/client_details.html', context)

Anyway, your help here will be much appreciated and if someone who is knowledgeable enough will be ok to do a skype call with me to explain to me I'd be very grateful.

Thanks

Jay

Farai M

unread,
Feb 6, 2020, 5:03:05 PM2/6/20
to django...@googlegroups.com
Put user id in your Entity table it will be user has one Entity one to one .In the view u just retrieve the Entity id related to that user.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ce7f4a92-e8e4-4837-9309-2ebdf243484a%40googlegroups.com.

Mariki Edward

unread,
Feb 7, 2020, 8:45:27 AM2/7/20
to django...@googlegroups.com
You should have user id in your entity model as a primary key and then the user id as a foreign key to invoice model, so that when the user is logged in you just get his/ her id from the entity model and use this id to retrieve the invoices that belong to the logged in user from invoice model.


Thanks,
Mariki Edward

--
Reply all
Reply to author
Forward
0 new messages