Associating ImageField url with another object in django
up vote
1
down vote
favorite
I have two models,
class Core:
def logo_path(instance, filename):
pattern = re.compile(r's+')
filename = re.sub(pattern, '', filename)
return 'images/core/logos/core_{0}/{1}'.format(instance.pk, filename)
name = models.CharField(db_index=True, max_length=255)
logo = models.ImageField(upload_to=logo_path, blank=True, null=True)
and
class CoreRoom(Room):
core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
I want to populate the logo_url in the CoreRoom object with the path to the logo in the Core object.
How do I do this?
python django
add a comment |
up vote
1
down vote
favorite
I have two models,
class Core:
def logo_path(instance, filename):
pattern = re.compile(r's+')
filename = re.sub(pattern, '', filename)
return 'images/core/logos/core_{0}/{1}'.format(instance.pk, filename)
name = models.CharField(db_index=True, max_length=255)
logo = models.ImageField(upload_to=logo_path, blank=True, null=True)
and
class CoreRoom(Room):
core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
I want to populate the logo_url in the CoreRoom object with the path to the logo in the Core object.
How do I do this?
python django
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have two models,
class Core:
def logo_path(instance, filename):
pattern = re.compile(r's+')
filename = re.sub(pattern, '', filename)
return 'images/core/logos/core_{0}/{1}'.format(instance.pk, filename)
name = models.CharField(db_index=True, max_length=255)
logo = models.ImageField(upload_to=logo_path, blank=True, null=True)
and
class CoreRoom(Room):
core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
I want to populate the logo_url in the CoreRoom object with the path to the logo in the Core object.
How do I do this?
python django
I have two models,
class Core:
def logo_path(instance, filename):
pattern = re.compile(r's+')
filename = re.sub(pattern, '', filename)
return 'images/core/logos/core_{0}/{1}'.format(instance.pk, filename)
name = models.CharField(db_index=True, max_length=255)
logo = models.ImageField(upload_to=logo_path, blank=True, null=True)
and
class CoreRoom(Room):
core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
I want to populate the logo_url in the CoreRoom object with the path to the logo in the Core object.
How do I do this?
python django
python django
edited Nov 8 at 3:15
Selcuk
25.8k75368
25.8k75368
asked Nov 8 at 2:21
Melissa Stewart
769622
769622
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
I doubt this is needed as . you can always access it using my_core_room.core.logo.url, but the easiest way would be to override the .save() method of your CoreRoom model:
class CoreRoom(Room):
...
def save(self, *args, **kwargs):
self.logo_url = self.core.logo.url
super(CoreRoom, self).save(*args, **kwargs)
add a comment |
up vote
1
down vote
logo_url model field can be converted to property method as well, because, it is not necessary to save logo url information as it is already stored in DB. For example:
class CoreRoom(Room):
core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
# remove logo url model field
@property
def logo_url(self):
return self.core.logo.url
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I doubt this is needed as . you can always access it using my_core_room.core.logo.url, but the easiest way would be to override the .save() method of your CoreRoom model:
class CoreRoom(Room):
...
def save(self, *args, **kwargs):
self.logo_url = self.core.logo.url
super(CoreRoom, self).save(*args, **kwargs)
add a comment |
up vote
2
down vote
accepted
I doubt this is needed as . you can always access it using my_core_room.core.logo.url, but the easiest way would be to override the .save() method of your CoreRoom model:
class CoreRoom(Room):
...
def save(self, *args, **kwargs):
self.logo_url = self.core.logo.url
super(CoreRoom, self).save(*args, **kwargs)
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I doubt this is needed as . you can always access it using my_core_room.core.logo.url, but the easiest way would be to override the .save() method of your CoreRoom model:
class CoreRoom(Room):
...
def save(self, *args, **kwargs):
self.logo_url = self.core.logo.url
super(CoreRoom, self).save(*args, **kwargs)
I doubt this is needed as . you can always access it using my_core_room.core.logo.url, but the easiest way would be to override the .save() method of your CoreRoom model:
class CoreRoom(Room):
...
def save(self, *args, **kwargs):
self.logo_url = self.core.logo.url
super(CoreRoom, self).save(*args, **kwargs)
answered Nov 8 at 2:41
Selcuk
25.8k75368
25.8k75368
add a comment |
add a comment |
up vote
1
down vote
logo_url model field can be converted to property method as well, because, it is not necessary to save logo url information as it is already stored in DB. For example:
class CoreRoom(Room):
core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
# remove logo url model field
@property
def logo_url(self):
return self.core.logo.url
add a comment |
up vote
1
down vote
logo_url model field can be converted to property method as well, because, it is not necessary to save logo url information as it is already stored in DB. For example:
class CoreRoom(Room):
core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
# remove logo url model field
@property
def logo_url(self):
return self.core.logo.url
add a comment |
up vote
1
down vote
up vote
1
down vote
logo_url model field can be converted to property method as well, because, it is not necessary to save logo url information as it is already stored in DB. For example:
class CoreRoom(Room):
core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
# remove logo url model field
@property
def logo_url(self):
return self.core.logo.url
logo_url model field can be converted to property method as well, because, it is not necessary to save logo url information as it is already stored in DB. For example:
class CoreRoom(Room):
core = models.ForeignKey(Core, on_delete=models.CASCADE, null=True)
# remove logo url model field
@property
def logo_url(self):
return self.core.logo.url
answered Nov 8 at 3:05
ruddra
9,62332547
9,62332547
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200648%2fassociating-imagefield-url-with-another-object-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown