0

When the form saves I want to save the object with a reference to the user's Profile page. So each profile can list every offer made by the user, see Class based views query: get objects referenced by another model

ofertoj_oferto.profile_id may not be NULL

My model of "Oferto" contains a ForeignKey to the User's Profile. Right now I don't have an idea how i can tell the system "Get the logged in User's Profile and save this Ofeto with that ID"

Maybe there is some way where I lookup the profile associated with the user and don't need both user and profile on the Oferto model.

But I will still need to a list of every oferto made by the user on there profile

profiles.models

class Profile(BaseInfo):
    bio = models.TextField(max_length=15000000)
    user = models.ForeignKey(User)

views.py

class OfertoCreateView(LoginRequiredMixin, Turtle_CreateView):
    model = Oferto
    action = "created"
    form_class = OfertoCreateForm

forms.py

class OfertoCreateForm(Turtle_Form):
    class Meta:
        model = Oferto
        fields = ("name",
                    "description",
                    "tags",
                    "time",
                    "requirements",
                    "location",
                    "image",)

models.py

class Oferto(models.Model):
    user = models.ForeignKey(User)
    profile = models.ForeignKey(Profile)

    name = models.CharField(max_length=150)
    description = models.TextField(max_length=3000)
    time = models.DecimalField(max_digits=10000000, decimal_places=2, null=True)
    stelo = models.DecimalField(max_digits=10000000, decimal_places=2, null=True)

    location = models.TextField(max_length=3000)

    slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('name',))
    tags = tagging.fields.TagField()

    image = models.ImageField(upload_to='Ofertoj', blank=True, null=True)

    requirements = models.TextField(max_length=550000, blank=True, null=True)

    def get_absolute_url(self):
        return reverse('oferto_detail', kwargs={'slug': self.slug})

    def __unicode__(self):
        return self.name

    def get_tags(self):
        return Tag.objects.get_for_object(self)
Community
  • 1
  • 1
Klanestro
  • 3,185
  • 5
  • 25
  • 31

1 Answers1

0

In your view/form/model save area: calling request.user returns a User object you can send to a model's ForeignKey field

Douglas Denhartog
  • 2,036
  • 1
  • 16
  • 23