On 8/3/06, Dave <[EMAIL PROTECTED]> wrote:
>
> I'm creating an application for a client in which the Admin has a
> "customer" model. The admin area was easy to set up, and everything's
> working just swell under Apache 2.
>
> The challenge I'm facing, however, is that when the Admin (admin here
> meaning a person using the Admin interface) creates a Customer (model),
> I want to automatically create a User (model) and assign permissions
> accordingly. I've read through the documentation, and I couldn't quite
> manage it.
>
> I tried what I thought was a bit clever, by using the User API on the
> Customer model, like so:
>
> # import User, etc.
>
> class Customer(models.Model):
>     # model.FieldsAndWhatnot
>     username = models.CharField(maxlength=50)
>     password = models.CharField(maxlength=50)
>     ...
>
>     user = User.objects.create_user(username, email, password)
>
> As sometimes is the case with clever solutions, this didn't work.
>
> Does anyone know how I can go about this? I don't want to do it via a
> view, and I don't want the Admins to have to manually create a User
> each time they add a Customer.
>
> The reason for all this is that I need to authenticate a Customer so
> they can see their Order (another model), but they should, somewhat
> obviously, not be able to see someone else's order.
>
> Have I explained this well? Does this make sense? Does anyone have any
> ideas?
>
> Big fan of Django, so far. Everything has been relatively easy to
> manage. This is the first honest challenge I've come across.
>


I think your best bet is to override the save() method of your model.
As an example, I have a model called Article, that has two main
fields: 'body' and 'body_html'.

In the admin, I enter the text into 'body' using Markdown syntax. On
save, I want it to automatically populate the body_html field with the
converted text.

class Article(models.Model):
    body = models.TextField()
    body_html = models.TextField(blank=True, editable=False)

    def save(self):
        self.body_html = markdown(self.body)
        super(Article, self).save()


So you could create the new User in the 'save' method.

Will that work for you?

Jay P.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to