0

Let's say I have two tables:

table#1: Space

enter image description here

table#2: Students

enter image description here

If I want to change the prom_name form table#1, I'd like to automatically update the prom_name in my table#2 as well

My model Students is a Form where students insert their name and the prom_code(given), if the prom_code does not exist then it Deny the registration

class Space(model.Models):
    prom_name = models.CharField(max_length = 20)
    prom_code = models.CharField(max_length = 20, Default=RANDOM, editable=FALSE)

    def save(self):
        try:
            From my_other_app.models import Students
            myobj = Students.object.get(prom_code = self.prom_code)
            myobj.prom_name = self.prom_name
            myobj.save()
        except Students.DoesNotExist:
        super(Space, self).save()

This is Just a simple example of what I'm trying to do, the original one its already validate if the register is new and so

I Just can't get how to change all my prom_names from every prom_code = self.prom_code (The one I'm changing) in my table Students

Do I need to use a for cycle?

Regards,

1
  • You have Prom_code and Prom_name in both tables. It looks like your database / models are not Normalized. If you normalize your database, you don't need to update Prom_name in each table if it is changed in one table. Commented May 20, 2016 at 17:48

2 Answers 2

1

Your design is not in compliance with 2nd normal form. The Prom_name should only exists in table Space but not in Students, because storing redundant field would only add additional disk space and increase maintenance work like the one you just described.

You should delete Prom_name column from Students table and do a join when you need to get the name with Space table.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right, since my prom_code is unique, I could just call my prom_name with the value I've or not change, Thanks alot, my mistake
1

You could normalize your database (by changing the models) in such a way:

table#1: Space

Prom_code  |   Prom_name
789             Prom1
800             Prom5
471             Prom3

table#2: Students

Name    |   ID    |    Prom_code
John         1           789
Martha       2           789   
Ramsy        3           800
Bob          4           471
Debbys       5           789

The Prom_code in Students table is a foreign key referring to Prom_code in Space table.

1 Comment

Thanks Alot my friend, I just realize my mistake in database design

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.